Skip to content

Commit

Permalink
Add Topology CRD
Browse files Browse the repository at this point in the history
Topology CRD is made by two different structs imported and abstracted
from PodSpec [1]:

1. TopologySpreadConstraint
2. Affinity

The above seems enough to draft a dedicated CR instead of exposing
those parameters through the service operators' API.
In addition, Affinity/AntiAffinity is imported from PodSpec [1] and
can override the default affinity policy defined in lib-common through
DistributePods function.

[1] https://pkg.go.dev/k8s.io/api/core/v1#PodSpec

Signed-off-by: Francesco Pantano <[email protected]>
  • Loading branch information
fmount committed Jan 16, 2025
1 parent f6db1ec commit b0bca79
Show file tree
Hide file tree
Showing 14 changed files with 2,664 additions and 0 deletions.
1,128 changes: 1,128 additions & 0 deletions apis/bases/topology.openstack.org_topologies.yaml

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions apis/topology/v1beta1/conditions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

import (
condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition"
)


// Common Messages used by API objects when a Topology is referenced
const (
// TopologyConfigReadyInitMessage
TopologyConfigReadyInitMessage = "Topology config create not started"
// TopologyConfigReadyMessage
TopologyConfigReadyMessage = "Topology config create completed"
// TopologyConfigReadyErrorMessage
TopologyConfigReadyErrorMessage = "Topology config create error occurred %s"
// TopologyConfigReadyCondition Status=True condition which indicates a CR
// exists and is referenced by the Service
TopologyConfigReadyCondition condition.Type = "TopologyConfigReady"
)
36 changes: 36 additions & 0 deletions apis/topology/v1beta1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2025.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package v1beta1 contains API Schema definitions for the topology v1beta1 API group
// +kubebuilder:object:generate=true
// +groupName=topology.openstack.org
package v1beta1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "topology.openstack.org", Version: "v1beta1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
108 changes: 108 additions & 0 deletions apis/topology/v1beta1/topology_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
Copyright 2024.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

import (
"context"
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
"k8s.io/apimachinery/pkg/util/validation/field"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

// TopologySpec defines the desired state of Topology
type TopologySpec struct {
// +kubebuilder:validation:Optional
// APITopologySpreadConstraint exposes topologySpreadConstraint that are
// applied to the StatefulSet
TopologySpreadConstraint *[]corev1.TopologySpreadConstraint `json:"topologySpreadConstraint,omitempty"`

// Affinity exposes PodAffinity, PodAntiaffinity and NodeAffinity overrides
// that are applied to StatefulSet/Deployments
// +optional
Affinity *corev1.Affinity `json:"affinity,omitempty"`

//TODO: We could add NodeSelector here as it belongs to the same APIGroup
}

// TopologyStatus defines the observed state of Topology
type TopologyStatus struct {

// ObservedGeneration - the most recent generation observed for this
// service.
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

// Topology is the Schema for the topologies API
type Topology struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec TopologySpec `json:"spec,omitempty"`
Status TopologyStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

// TopologyList contains a list of Topology
type TopologyList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Topology `json:"items"`
}

func init() {
SchemeBuilder.Register(&Topology{}, &TopologyList{})
}

// GetTopologyByName - a function exposed to the service operators
// that need to retrieve the referenced topology by name
func GetTopologyByName(
ctx context.Context,
h *helper.Helper,
name string,
namespace string,
) (*Topology, string, error) {

topology := &Topology{}
var hash string = ""

err := h.GetClient().Get(ctx, types.NamespacedName{Name: name, Namespace: namespace}, topology)
if err != nil {
return topology, "", err
}
hash, err = util.ObjectHash(topology.Spec)
if err != nil {
return topology, "", err
}
return topology, hash, nil
}

// ValidateTopologyNamespace - returns a field.Error when the Service
// references a Topoology deployed on a different namespace
func ValidateTopologyNamespace(refNs string, basePath field.Path, validNs string) (*field.Error) {
if refNs != "" && refNs != validNs {
topologyNamespace := basePath.Child("topology").Key("namespace")
return field.Invalid(topologyNamespace, "namespace", "Customizing namespace field is not supported")
}
return nil
}
131 changes: 131 additions & 0 deletions apis/topology/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b0bca79

Please sign in to comment.