From cca3d0c309e3ecc14bb64e71a9701b1ea2156c96 Mon Sep 17 00:00:00 2001 From: Enrico Candino Date: Thu, 15 May 2025 12:04:47 +0200 Subject: [PATCH] Rename `ClusterSet` to `VirtualClusterPolicy` (#349) * rename clusterset to policy * fixes --- ...aml => k3k.io_virtualclusterpolicies.yaml} | 31 +-- charts/k3k/values.yaml | 2 +- cli/cmds/cluster_create.go | 26 +- cli/cmds/cluster_create_flags.go | 6 +- cli/cmds/clusterset.go | 16 -- cli/cmds/policy.go | 16 ++ ...{clusterset_create.go => policy_create.go} | 36 +-- ...{clusterset_delete.go => policy_delete.go} | 18 +- cli/cmds/root.go | 2 +- docs/cli/cli-docs.md | 20 +- docs/crds/config.yaml | 4 +- examples/clusterset.yaml | 4 +- main.go | 14 +- pkg/apis/k3k.io/v1alpha1/register.go | 4 +- pkg/apis/k3k.io/v1alpha1/types.go | 40 +-- .../k3k.io/v1alpha1/zz_generated.deepcopy.go | 246 +++++++++--------- pkg/controller/{clusterset => policy}/node.go | 20 +- .../clusterset.go => policy/policy.go} | 130 ++++----- .../policy_suite_test.go} | 8 +- .../policy_test.go} | 204 +++++++-------- 20 files changed, 424 insertions(+), 423 deletions(-) rename charts/k3k/crds/{k3k.io_clustersets.yaml => k3k.io_virtualclusterpolicies.yaml} (95%) delete mode 100644 cli/cmds/clusterset.go create mode 100644 cli/cmds/policy.go rename cli/cmds/{clusterset_create.go => policy_create.go} (69%) rename cli/cmds/{clusterset_delete.go => policy_delete.go} (64%) rename pkg/controller/{clusterset => policy}/node.go (78%) rename pkg/controller/{clusterset/clusterset.go => policy/policy.go} (65%) rename pkg/controller/{clusterset/clusterset_suite_test.go => policy/policy_suite_test.go} (92%) rename pkg/controller/{clusterset/clusterset_test.go => policy/policy_test.go} (79%) diff --git a/charts/k3k/crds/k3k.io_clustersets.yaml b/charts/k3k/crds/k3k.io_virtualclusterpolicies.yaml similarity index 95% rename from charts/k3k/crds/k3k.io_clustersets.yaml rename to charts/k3k/crds/k3k.io_virtualclusterpolicies.yaml index 903bf088..636f2475 100644 --- a/charts/k3k/crds/k3k.io_clustersets.yaml +++ b/charts/k3k/crds/k3k.io_virtualclusterpolicies.yaml @@ -4,14 +4,14 @@ kind: CustomResourceDefinition metadata: annotations: controller-gen.kubebuilder.io/version: v0.14.0 - name: clustersets.k3k.io + name: virtualclusterpolicies.k3k.io spec: group: k3k.io names: - kind: ClusterSet - listKind: ClusterSetList - plural: clustersets - singular: clusterset + kind: VirtualClusterPolicy + listKind: VirtualClusterPolicyList + plural: virtualclusterpolicies + singular: virtualclusterpolicy scope: Namespaced versions: - additionalPrinterColumns: @@ -25,8 +25,8 @@ spec: schema: openAPIV3Schema: description: |- - ClusterSet represents a group of virtual Kubernetes clusters managed by k3k. - It allows defining common configurations and constraints for the clusters within the set. + VirtualClusterPolicy allows defining common configurations and constraints + for the clusters in a namespace activated by an annotation on that Namespace. properties: apiVersion: description: |- @@ -47,7 +47,7 @@ spec: type: object spec: default: {} - description: Spec defines the desired state of the ClusterSet. + description: Spec defines the desired state of the VirtualClusterPolicy. properties: allowedModeTypes: default: @@ -70,22 +70,22 @@ spec: additionalProperties: type: string description: DefaultNodeSelector specifies the node selector that - applies to all clusters (server + agent) in the set. + applies to all clusters (server + agent) in the target Namespace. type: object defaultPriorityClass: description: DefaultPriorityClass specifies the priorityClassName - applied to all pods of all clusters in the set. + applied to all pods of all clusters in the target Namespace. type: string disableNetworkPolicy: description: DisableNetworkPolicy indicates whether to disable the creation of a default network policy for cluster isolation. type: boolean displayName: - description: DisplayName is the human-readable name for the set. + description: DisplayName is the human-readable name for the policy. type: string limit: description: |- - Limit specifies the LimitRange that will be applied to all pods within the ClusterSet + Limit specifies the LimitRange that will be applied to all pods in the target Namespace to set defaults and constraints (min/max) properties: limits: @@ -168,8 +168,8 @@ spec: - restricted type: string quota: - description: Quota specifies the resource limits for clusters within - a clusterset. + description: Quota defines the ResourceQuotaSpec to be applied to + the target Namespace. properties: hard: additionalProperties: @@ -233,7 +233,7 @@ spec: type: object type: object status: - description: Status reflects the observed state of the ClusterSet. + description: Status reflects the observed state of the VirtualClusterPolicy. properties: conditions: description: Conditions are the individual conditions for the cluster @@ -320,6 +320,7 @@ spec: type: string type: object required: + - metadata - spec type: object x-kubernetes-validations: diff --git a/charts/k3k/values.yaml b/charts/k3k/values.yaml index 71f6ca54..e7eceab6 100644 --- a/charts/k3k/values.yaml +++ b/charts/k3k/values.yaml @@ -10,7 +10,7 @@ nameOverride: "" fullnameOverride: "" host: - # clusterCIDR specifies the clusterCIDR that will be added to the default networkpolicy for clustersets, if not set + # clusterCIDR specifies the clusterCIDR that will be added to the default networkpolicy, if not set # the controller will collect the PodCIDRs of all the nodes on the system. clusterCIDR: "" diff --git a/cli/cmds/cluster_create.go b/cli/cmds/cluster_create.go index 9ee559a9..96d67796 100644 --- a/cli/cmds/cluster_create.go +++ b/cli/cmds/cluster_create.go @@ -39,7 +39,7 @@ type CreateConfig struct { version string mode string kubeconfigServerHost string - clusterset string + policy string } func NewClusterCreateCmd(appCtx *AppContext) *cli.Command { @@ -76,35 +76,35 @@ func createAction(appCtx *AppContext, config *CreateConfig) cli.ActionFunc { namespace := appCtx.Namespace(name) - // if clusterset is set, use the namespace of the clusterset - if config.clusterset != "" { - namespace = appCtx.Namespace(config.clusterset) + // if policy is set, use the namespace of the policy + if config.policy != "" { + namespace = appCtx.Namespace(config.policy) } if err := createNamespace(ctx, client, namespace); err != nil { return err } - // if clusterset is set, create the cluster set - if config.clusterset != "" { - namespace = appCtx.Namespace(config.clusterset) + // if policy is set, create the cluster set + if config.policy != "" { + namespace = appCtx.Namespace(config.policy) - clusterSet := &v1alpha1.ClusterSet{} - if err := client.Get(ctx, types.NamespacedName{Name: "default", Namespace: namespace}, clusterSet); err != nil { + policy := &v1alpha1.VirtualClusterPolicy{} + if err := client.Get(ctx, types.NamespacedName{Name: "default", Namespace: namespace}, policy); err != nil { if !apierrors.IsNotFound(err) { return err } - clusterSet, err = createClusterSet(ctx, client, namespace, v1alpha1.ClusterMode(config.mode), config.clusterset) + policy, err = createPolicy(ctx, client, namespace, v1alpha1.ClusterMode(config.mode), config.policy) if err != nil { return err } } - logrus.Infof("ClusterSet in namespace [%s] available", namespace) + logrus.Infof("VirtualClusterPolicy in namespace [%s] available", namespace) - if !slices.Contains(clusterSet.Spec.AllowedModeTypes, v1alpha1.ClusterMode(config.mode)) { - return fmt.Errorf("invalid '%s' Cluster mode. ClusterSet only allows %v", config.mode, clusterSet.Spec.AllowedModeTypes) + if !slices.Contains(policy.Spec.AllowedModeTypes, v1alpha1.ClusterMode(config.mode)) { + return fmt.Errorf("invalid '%s' Cluster mode. VirtualClusterPolicy only allows %v", config.mode, policy.Spec.AllowedModeTypes) } } diff --git a/cli/cmds/cluster_create_flags.go b/cli/cmds/cluster_create_flags.go index 18093539..6b9b50d0 100644 --- a/cli/cmds/cluster_create_flags.go +++ b/cli/cmds/cluster_create_flags.go @@ -105,9 +105,9 @@ func NewCreateFlags(config *CreateConfig) []cli.Flag { Destination: &config.kubeconfigServerHost, }, &cli.StringFlag{ - Name: "clusterset", - Usage: "The clusterset to create the cluster in", - Destination: &config.clusterset, + Name: "policy", + Usage: "The policy to create the cluster in", + Destination: &config.policy, }, } } diff --git a/cli/cmds/clusterset.go b/cli/cmds/clusterset.go deleted file mode 100644 index 42c0d53e..00000000 --- a/cli/cmds/clusterset.go +++ /dev/null @@ -1,16 +0,0 @@ -package cmds - -import ( - "github.com/urfave/cli/v2" -) - -func NewClusterSetCmd(appCtx *AppContext) *cli.Command { - return &cli.Command{ - Name: "clusterset", - Usage: "clusterset command", - Subcommands: []*cli.Command{ - NewClusterSetCreateCmd(appCtx), - NewClusterSetDeleteCmd(appCtx), - }, - } -} diff --git a/cli/cmds/policy.go b/cli/cmds/policy.go new file mode 100644 index 00000000..d8231f8e --- /dev/null +++ b/cli/cmds/policy.go @@ -0,0 +1,16 @@ +package cmds + +import ( + "github.com/urfave/cli/v2" +) + +func NewPolicyCmd(appCtx *AppContext) *cli.Command { + return &cli.Command{ + Name: "policy", + Usage: "policy command", + Subcommands: []*cli.Command{ + NewPolicyCreateCmd(appCtx), + NewPolicyDeleteCmd(appCtx), + }, + } +} diff --git a/cli/cmds/clusterset_create.go b/cli/cmds/policy_create.go similarity index 69% rename from cli/cmds/clusterset_create.go rename to cli/cmds/policy_create.go index 02f7f8bd..f1c28fef 100644 --- a/cli/cmds/clusterset_create.go +++ b/cli/cmds/policy_create.go @@ -15,18 +15,18 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) -type ClusterSetCreateConfig struct { +type VirtualClusterPolicyCreateConfig struct { mode string displayName string } -func NewClusterSetCreateCmd(appCtx *AppContext) *cli.Command { - config := &ClusterSetCreateConfig{} +func NewPolicyCreateCmd(appCtx *AppContext) *cli.Command { + config := &VirtualClusterPolicyCreateConfig{} createFlags := []cli.Flag{ &cli.StringFlag{ Name: "mode", - Usage: "The allowed mode type of the clusterset", + Usage: "The allowed mode type of the policy", Destination: &config.mode, Value: "shared", Action: func(ctx *cli.Context, value string) error { @@ -40,22 +40,22 @@ func NewClusterSetCreateCmd(appCtx *AppContext) *cli.Command { }, &cli.StringFlag{ Name: "display-name", - Usage: "The display name of the clusterset", + Usage: "The display name of the policy", Destination: &config.displayName, }, } return &cli.Command{ Name: "create", - Usage: "Create new clusterset", - UsageText: "k3kcli clusterset create [command options] NAME", - Action: clusterSetCreateAction(appCtx, config), + Usage: "Create new policy", + UsageText: "k3kcli policy create [command options] NAME", + Action: policyCreateAction(appCtx, config), Flags: WithCommonFlags(appCtx, createFlags...), HideHelpCommand: true, } } -func clusterSetCreateAction(appCtx *AppContext, config *ClusterSetCreateConfig) cli.ActionFunc { +func policyCreateAction(appCtx *AppContext, config *VirtualClusterPolicyCreateConfig) cli.ActionFunc { return func(clx *cli.Context) error { ctx := context.Background() client := appCtx.Client @@ -85,7 +85,7 @@ func clusterSetCreateAction(appCtx *AppContext, config *ClusterSetCreateConfig) return err } - _, err := createClusterSet(ctx, client, namespace, v1alpha1.ClusterMode(config.mode), displayName) + _, err := createPolicy(ctx, client, namespace, v1alpha1.ClusterMode(config.mode), displayName) return err } @@ -108,31 +108,31 @@ func createNamespace(ctx context.Context, client client.Client, name string) err return nil } -func createClusterSet(ctx context.Context, client client.Client, namespace string, mode v1alpha1.ClusterMode, displayName string) (*v1alpha1.ClusterSet, error) { - logrus.Infof("Creating clusterset in namespace [%s]", namespace) +func createPolicy(ctx context.Context, client client.Client, namespace string, mode v1alpha1.ClusterMode, displayName string) (*v1alpha1.VirtualClusterPolicy, error) { + logrus.Infof("Creating policy in namespace [%s]", namespace) - clusterSet := &v1alpha1.ClusterSet{ + policy := &v1alpha1.VirtualClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ Name: "default", Namespace: namespace, }, TypeMeta: metav1.TypeMeta{ - Kind: "ClusterSet", + Kind: "VirtualClusterPolicy", APIVersion: "k3k.io/v1alpha1", }, - Spec: v1alpha1.ClusterSetSpec{ + Spec: v1alpha1.VirtualClusterPolicySpec{ AllowedModeTypes: []v1alpha1.ClusterMode{mode}, DisplayName: displayName, }, } - if err := client.Create(ctx, clusterSet); err != nil { + if err := client.Create(ctx, policy); err != nil { if apierrors.IsAlreadyExists(err) { - logrus.Infof("ClusterSet in namespace [%s] already exists", namespace) + logrus.Infof("Policy in namespace [%s] already exists", namespace) } else { return nil, err } } - return clusterSet, nil + return policy, nil } diff --git a/cli/cmds/clusterset_delete.go b/cli/cmds/policy_delete.go similarity index 64% rename from cli/cmds/clusterset_delete.go rename to cli/cmds/policy_delete.go index 97eba364..f1e8fbde 100644 --- a/cli/cmds/clusterset_delete.go +++ b/cli/cmds/policy_delete.go @@ -12,18 +12,18 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -func NewClusterSetDeleteCmd(appCtx *AppContext) *cli.Command { +func NewPolicyDeleteCmd(appCtx *AppContext) *cli.Command { return &cli.Command{ Name: "delete", - Usage: "Delete an existing clusterset", - UsageText: "k3kcli clusterset delete [command options] NAME", - Action: clusterSetDeleteAction(appCtx), + Usage: "Delete an existing policy", + UsageText: "k3kcli policy delete [command options] NAME", + Action: policyDeleteAction(appCtx), Flags: WithCommonFlags(appCtx), HideHelpCommand: true, } } -func clusterSetDeleteAction(appCtx *AppContext) cli.ActionFunc { +func policyDeleteAction(appCtx *AppContext) cli.ActionFunc { return func(clx *cli.Context) error { ctx := context.Background() client := appCtx.Client @@ -39,18 +39,18 @@ func clusterSetDeleteAction(appCtx *AppContext) cli.ActionFunc { namespace := appCtx.Namespace(name) - logrus.Infof("Deleting clusterset in namespace [%s]", namespace) + logrus.Infof("Deleting policy in namespace [%s]", namespace) - clusterSet := &v1alpha1.ClusterSet{ + policy := &v1alpha1.VirtualClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ Name: "default", Namespace: namespace, }, } - if err := client.Delete(ctx, clusterSet); err != nil { + if err := client.Delete(ctx, policy); err != nil { if apierrors.IsNotFound(err) { - logrus.Warnf("ClusterSet not found in namespace [%s]", namespace) + logrus.Warnf("Policy not found in namespace [%s]", namespace) } else { return err } diff --git a/cli/cmds/root.go b/cli/cmds/root.go index c60a247c..e8e1c649 100644 --- a/cli/cmds/root.go +++ b/cli/cmds/root.go @@ -64,7 +64,7 @@ func NewApp() *cli.App { app.Commands = []*cli.Command{ NewClusterCmd(appCtx), - NewClusterSetCmd(appCtx), + NewPolicyCmd(appCtx), NewKubeconfigCmd(appCtx), } diff --git a/docs/cli/cli-docs.md b/docs/cli/cli-docs.md index 0f67bcde..b80aa2c6 100644 --- a/docs/cli/cli-docs.md +++ b/docs/cli/cli-docs.md @@ -47,8 +47,6 @@ Create new cluster **--cluster-cidr**="": cluster CIDR -**--clusterset**="": The clusterset to create the cluster in - **--debug**: Turn on debug logs **--kubeconfig**="": kubeconfig path (default: $HOME/.kube/config or $KUBECONFIG if set) @@ -61,6 +59,8 @@ Create new cluster **--persistence-type**="": persistence mode for the nodes (dynamic, ephemeral, static) (default: "dynamic") +**--policy**="": The policy to create the cluster in + **--server-args**="": servers extra arguments **--server-envs**="": servers extra Envs @@ -89,31 +89,31 @@ Delete an existing cluster **--namespace**="": namespace to create the k3k cluster in -## clusterset +## policy -clusterset command +policy command ### create -Create new clusterset +Create new policy ->k3kcli clusterset create [command options] NAME +>k3kcli policy create [command options] NAME **--debug**: Turn on debug logs -**--display-name**="": The display name of the clusterset +**--display-name**="": The display name of the policy **--kubeconfig**="": kubeconfig path (default: $HOME/.kube/config or $KUBECONFIG if set) -**--mode**="": The allowed mode type of the clusterset (default: "shared") +**--mode**="": The allowed mode type of the policy (default: "shared") **--namespace**="": namespace to create the k3k cluster in ### delete -Delete an existing clusterset +Delete an existing policy ->k3kcli clusterset delete [command options] NAME +>k3kcli policy delete [command options] NAME **--debug**: Turn on debug logs diff --git a/docs/crds/config.yaml b/docs/crds/config.yaml index 981a835c..d6cb3b05 100644 --- a/docs/crds/config.yaml +++ b/docs/crds/config.yaml @@ -1,8 +1,8 @@ processor: # RE2 regular expressions describing types that should be excluded from the generated documentation. ignoreTypes: - - ClusterSet - - ClusterSetList + - VirtualClusterPolicy + - VirtualClusterPolicyList # RE2 regular expressions describing type fields that should be excluded from the generated documentation. ignoreFields: diff --git a/examples/clusterset.yaml b/examples/clusterset.yaml index 22e93204..19a28a0a 100644 --- a/examples/clusterset.yaml +++ b/examples/clusterset.yaml @@ -1,7 +1,7 @@ apiVersion: k3k.io/v1alpha1 -kind: ClusterSet +kind: VirtualClusterPolicy metadata: - name: clusterset-example + name: policy-example # spec: # disableNetworkPolicy: false # allowedModeTypes: diff --git a/main.go b/main.go index 63677b5a..8f3775cd 100644 --- a/main.go +++ b/main.go @@ -12,7 +12,7 @@ import ( "github.com/rancher/k3k/pkg/apis/k3k.io/v1alpha1" "github.com/rancher/k3k/pkg/buildinfo" "github.com/rancher/k3k/pkg/controller/cluster" - "github.com/rancher/k3k/pkg/controller/clusterset" + "github.com/rancher/k3k/pkg/controller/policy" "github.com/rancher/k3k/pkg/log" "github.com/urfave/cli/v2" "go.uber.org/zap" @@ -43,7 +43,7 @@ var ( &cli.StringFlag{ Name: "cluster-cidr", EnvVars: []string{"CLUSTER_CIDR"}, - Usage: "Cluster CIDR to be added to the networkpolicy of the clustersets", + Usage: "Cluster CIDR to be added to the networkpolicy", Destination: &clusterCIDR, }, &cli.StringFlag{ @@ -125,17 +125,17 @@ func run(clx *cli.Context) error { return fmt.Errorf("failed to add the new cluster controller: %v", err) } - logger.Info("adding clusterset controller") + logger.Info("adding clusterpolicy controller") - if err := clusterset.Add(ctx, mgr, clusterCIDR); err != nil { - return fmt.Errorf("failed to add the clusterset controller: %v", err) + if err := policy.Add(ctx, mgr, clusterCIDR); err != nil { + return fmt.Errorf("failed to add the clusterpolicy controller: %v", err) } if clusterCIDR == "" { logger.Info("adding networkpolicy node controller") - if err := clusterset.AddNodeController(ctx, mgr); err != nil { - return fmt.Errorf("failed to add the clusterset node controller: %v", err) + if err := policy.AddNodeController(ctx, mgr); err != nil { + return fmt.Errorf("failed to add the clusterpolicy node controller: %v", err) } } diff --git a/pkg/apis/k3k.io/v1alpha1/register.go b/pkg/apis/k3k.io/v1alpha1/register.go index 6f531dd5..2cfe03c5 100644 --- a/pkg/apis/k3k.io/v1alpha1/register.go +++ b/pkg/apis/k3k.io/v1alpha1/register.go @@ -21,8 +21,8 @@ func addKnownTypes(s *runtime.Scheme) error { s.AddKnownTypes(SchemeGroupVersion, &Cluster{}, &ClusterList{}, - &ClusterSet{}, - &ClusterSetList{}, + &VirtualClusterPolicy{}, + &VirtualClusterPolicyList{}, ) metav1.AddToGroupVersion(s, SchemeGroupVersion) diff --git a/pkg/apis/k3k.io/v1alpha1/types.go b/pkg/apis/k3k.io/v1alpha1/types.go index d7f0a7d8..dd898a50 100644 --- a/pkg/apis/k3k.io/v1alpha1/types.go +++ b/pkg/apis/k3k.io/v1alpha1/types.go @@ -337,48 +337,48 @@ type ClusterList struct { // +kubebuilder:printcolumn:JSONPath=".spec.displayName",name=Display Name,type=string // +kubebuilder:printcolumn:JSONPath=".metadata.creationTimestamp",name=Age,type=date -// ClusterSet represents a group of virtual Kubernetes clusters managed by k3k. -// It allows defining common configurations and constraints for the clusters within the set. -type ClusterSet struct { - metav1.ObjectMeta `json:"metadata,omitempty"` +// VirtualClusterPolicy allows defining common configurations and constraints +// for the clusters in a namespace activated by an annotation on that Namespace. +type VirtualClusterPolicy struct { + metav1.ObjectMeta `json:"metadata"` metav1.TypeMeta `json:",inline"` - // Spec defines the desired state of the ClusterSet. + // Spec defines the desired state of the VirtualClusterPolicy. // // +kubebuilder:default={} - Spec ClusterSetSpec `json:"spec"` + Spec VirtualClusterPolicySpec `json:"spec"` - // Status reflects the observed state of the ClusterSet. + // Status reflects the observed state of the VirtualClusterPolicy. // // +optional - Status ClusterSetStatus `json:"status,omitempty"` + Status VirtualClusterPolicyStatus `json:"status"` } -// ClusterSetSpec defines the desired state of a ClusterSet. -type ClusterSetSpec struct { +// VirtualClusterPolicySpec defines the desired state of a VirtualClusterPolicy. +type VirtualClusterPolicySpec struct { - // DisplayName is the human-readable name for the set. + // DisplayName is the human-readable name for the policy. // // +optional DisplayName string `json:"displayName,omitempty"` - // Quota specifies the resource limits for clusters within a clusterset. + // Quota defines the ResourceQuotaSpec to be applied to the target Namespace. // // +optional Quota *v1.ResourceQuotaSpec `json:"quota,omitempty"` - // Limit specifies the LimitRange that will be applied to all pods within the ClusterSet + // Limit specifies the LimitRange that will be applied to all pods in the target Namespace // to set defaults and constraints (min/max) // // +optional Limit *v1.LimitRangeSpec `json:"limit,omitempty"` - // DefaultNodeSelector specifies the node selector that applies to all clusters (server + agent) in the set. + // DefaultNodeSelector specifies the node selector that applies to all clusters (server + agent) in the target Namespace. // // +optional DefaultNodeSelector map[string]string `json:"defaultNodeSelector,omitempty"` - // DefaultPriorityClass specifies the priorityClassName applied to all pods of all clusters in the set. + // DefaultPriorityClass specifies the priorityClassName applied to all pods of all clusters in the target Namespace. // // +optional DefaultPriorityClass string `json:"defaultPriorityClass,omitempty"` @@ -418,8 +418,8 @@ const ( RestrictedPodSecurityAdmissionLevel = PodSecurityAdmissionLevel("restricted") ) -// ClusterSetStatus reflects the observed state of a ClusterSet. -type ClusterSetStatus struct { +// VirtualClusterPolicyStatus reflects the observed state of a VirtualClusterPolicy. +type VirtualClusterPolicyStatus struct { // ObservedGeneration was the generation at the time the status was updated. // // +optional @@ -446,10 +446,10 @@ type ClusterSetStatus struct { // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // +kubebuilder:object:root=true -// ClusterSetList is a list of ClusterSet resources. -type ClusterSetList struct { +// VirtualClusterPolicyList is a list of VirtualClusterPolicy resources. +type VirtualClusterPolicyList struct { metav1.ListMeta `json:"metadata,omitempty"` metav1.TypeMeta `json:",inline"` - Items []ClusterSet `json:"items"` + Items []VirtualClusterPolicy `json:"items"` } diff --git a/pkg/apis/k3k.io/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/k3k.io/v1alpha1/zz_generated.deepcopy.go index b4ede03b..448f301b 100644 --- a/pkg/apis/k3k.io/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/k3k.io/v1alpha1/zz_generated.deepcopy.go @@ -84,129 +84,6 @@ func (in *ClusterList) DeepCopyObject() runtime.Object { return nil } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *ClusterSet) DeepCopyInto(out *ClusterSet) { - *out = *in - in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) - out.TypeMeta = in.TypeMeta - in.Spec.DeepCopyInto(&out.Spec) - in.Status.DeepCopyInto(&out.Status) -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ClusterSet. -func (in *ClusterSet) DeepCopy() *ClusterSet { - if in == nil { - return nil - } - out := new(ClusterSet) - in.DeepCopyInto(out) - return out -} - -// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (in *ClusterSet) DeepCopyObject() runtime.Object { - if c := in.DeepCopy(); c != nil { - return c - } - return nil -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *ClusterSetList) DeepCopyInto(out *ClusterSetList) { - *out = *in - in.ListMeta.DeepCopyInto(&out.ListMeta) - out.TypeMeta = in.TypeMeta - if in.Items != nil { - in, out := &in.Items, &out.Items - *out = make([]ClusterSet, len(*in)) - for i := range *in { - (*in)[i].DeepCopyInto(&(*out)[i]) - } - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ClusterSetList. -func (in *ClusterSetList) DeepCopy() *ClusterSetList { - if in == nil { - return nil - } - out := new(ClusterSetList) - in.DeepCopyInto(out) - return out -} - -// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (in *ClusterSetList) DeepCopyObject() runtime.Object { - if c := in.DeepCopy(); c != nil { - return c - } - return nil -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *ClusterSetSpec) DeepCopyInto(out *ClusterSetSpec) { - *out = *in - if in.Quota != nil { - in, out := &in.Quota, &out.Quota - *out = new(v1.ResourceQuotaSpec) - (*in).DeepCopyInto(*out) - } - if in.Limit != nil { - in, out := &in.Limit, &out.Limit - *out = new(v1.LimitRangeSpec) - (*in).DeepCopyInto(*out) - } - if in.DefaultNodeSelector != nil { - in, out := &in.DefaultNodeSelector, &out.DefaultNodeSelector - *out = make(map[string]string, len(*in)) - for key, val := range *in { - (*out)[key] = val - } - } - if in.AllowedModeTypes != nil { - in, out := &in.AllowedModeTypes, &out.AllowedModeTypes - *out = make([]ClusterMode, len(*in)) - copy(*out, *in) - } - if in.PodSecurityAdmissionLevel != nil { - in, out := &in.PodSecurityAdmissionLevel, &out.PodSecurityAdmissionLevel - *out = new(PodSecurityAdmissionLevel) - **out = **in - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ClusterSetSpec. -func (in *ClusterSetSpec) DeepCopy() *ClusterSetSpec { - if in == nil { - return nil - } - out := new(ClusterSetSpec) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *ClusterSetStatus) DeepCopyInto(out *ClusterSetStatus) { - *out = *in - if in.Conditions != nil { - in, out := &in.Conditions, &out.Conditions - *out = make([]metav1.Condition, len(*in)) - for i := range *in { - (*in)[i].DeepCopyInto(&(*out)[i]) - } - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ClusterSetStatus. -func (in *ClusterSetStatus) DeepCopy() *ClusterSetStatus { - if in == nil { - return nil - } - out := new(ClusterSetStatus) - in.DeepCopyInto(out) - return out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ClusterSpec) DeepCopyInto(out *ClusterSpec) { *out = *in @@ -440,3 +317,126 @@ func (in *PersistenceConfig) DeepCopy() *PersistenceConfig { in.DeepCopyInto(out) return out } + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *VirtualClusterPolicy) DeepCopyInto(out *VirtualClusterPolicy) { + *out = *in + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + out.TypeMeta = in.TypeMeta + in.Spec.DeepCopyInto(&out.Spec) + in.Status.DeepCopyInto(&out.Status) +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new VirtualClusterPolicy. +func (in *VirtualClusterPolicy) DeepCopy() *VirtualClusterPolicy { + if in == nil { + return nil + } + out := new(VirtualClusterPolicy) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *VirtualClusterPolicy) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *VirtualClusterPolicyList) DeepCopyInto(out *VirtualClusterPolicyList) { + *out = *in + in.ListMeta.DeepCopyInto(&out.ListMeta) + out.TypeMeta = in.TypeMeta + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]VirtualClusterPolicy, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new VirtualClusterPolicyList. +func (in *VirtualClusterPolicyList) DeepCopy() *VirtualClusterPolicyList { + if in == nil { + return nil + } + out := new(VirtualClusterPolicyList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *VirtualClusterPolicyList) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *VirtualClusterPolicySpec) DeepCopyInto(out *VirtualClusterPolicySpec) { + *out = *in + if in.Quota != nil { + in, out := &in.Quota, &out.Quota + *out = new(v1.ResourceQuotaSpec) + (*in).DeepCopyInto(*out) + } + if in.Limit != nil { + in, out := &in.Limit, &out.Limit + *out = new(v1.LimitRangeSpec) + (*in).DeepCopyInto(*out) + } + if in.DefaultNodeSelector != nil { + in, out := &in.DefaultNodeSelector, &out.DefaultNodeSelector + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } + if in.AllowedModeTypes != nil { + in, out := &in.AllowedModeTypes, &out.AllowedModeTypes + *out = make([]ClusterMode, len(*in)) + copy(*out, *in) + } + if in.PodSecurityAdmissionLevel != nil { + in, out := &in.PodSecurityAdmissionLevel, &out.PodSecurityAdmissionLevel + *out = new(PodSecurityAdmissionLevel) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new VirtualClusterPolicySpec. +func (in *VirtualClusterPolicySpec) DeepCopy() *VirtualClusterPolicySpec { + if in == nil { + return nil + } + out := new(VirtualClusterPolicySpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *VirtualClusterPolicyStatus) DeepCopyInto(out *VirtualClusterPolicyStatus) { + *out = *in + if in.Conditions != nil { + in, out := &in.Conditions, &out.Conditions + *out = make([]metav1.Condition, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new VirtualClusterPolicyStatus. +func (in *VirtualClusterPolicyStatus) DeepCopy() *VirtualClusterPolicyStatus { + if in == nil { + return nil + } + out := new(VirtualClusterPolicyStatus) + in.DeepCopyInto(out) + return out +} diff --git a/pkg/controller/clusterset/node.go b/pkg/controller/policy/node.go similarity index 78% rename from pkg/controller/clusterset/node.go rename to pkg/controller/policy/node.go index 6c78a102..948b5197 100644 --- a/pkg/controller/clusterset/node.go +++ b/pkg/controller/policy/node.go @@ -1,4 +1,4 @@ -package clusterset +package policy import ( "context" @@ -47,35 +47,35 @@ func (n *NodeReconciler) Reconcile(ctx context.Context, req reconcile.Request) ( log.Info("reconciling node") - var clusterSetList v1alpha1.ClusterSetList - if err := n.Client.List(ctx, &clusterSetList); err != nil { + var clusterPolicyList v1alpha1.VirtualClusterPolicyList + if err := n.Client.List(ctx, &clusterPolicyList); err != nil { return reconcile.Result{}, err } - if len(clusterSetList.Items) <= 0 { + if len(clusterPolicyList.Items) <= 0 { return reconcile.Result{}, nil } - if err := n.ensureNetworkPolicies(ctx, clusterSetList); err != nil { + if err := n.ensureNetworkPolicies(ctx, clusterPolicyList); err != nil { return reconcile.Result{}, err } return reconcile.Result{}, nil } -func (n *NodeReconciler) ensureNetworkPolicies(ctx context.Context, clusterSetList v1alpha1.ClusterSetList) error { +func (n *NodeReconciler) ensureNetworkPolicies(ctx context.Context, clusterPolicyList v1alpha1.VirtualClusterPolicyList) error { log := ctrl.LoggerFrom(ctx) log.Info("ensuring network policies") var setNetworkPolicy *networkingv1.NetworkPolicy - for _, cs := range clusterSetList.Items { + for _, cs := range clusterPolicyList.Items { if cs.Spec.DisableNetworkPolicy { continue } - log = log.WithValues("clusterset", cs.Namespace+"/"+cs.Name) - log.Info("updating NetworkPolicy for ClusterSet") + log = log.WithValues("clusterpolicy", cs.Namespace+"/"+cs.Name) + log.Info("updating NetworkPolicy for VirtualClusterPolicy") var err error setNetworkPolicy, err = netpol(ctx, "", &cs, n.Client) @@ -84,7 +84,7 @@ func (n *NodeReconciler) ensureNetworkPolicies(ctx context.Context, clusterSetLi return err } - log.Info("new NetworkPolicy for clusterset") + log.Info("new NetworkPolicy for clusterpolicy") if err := n.Client.Update(ctx, setNetworkPolicy); err != nil { return err diff --git a/pkg/controller/clusterset/clusterset.go b/pkg/controller/policy/policy.go similarity index 65% rename from pkg/controller/clusterset/clusterset.go rename to pkg/controller/policy/policy.go index f72dfed6..db338f23 100644 --- a/pkg/controller/clusterset/clusterset.go +++ b/pkg/controller/policy/policy.go @@ -1,4 +1,4 @@ -package clusterset +package policy import ( "context" @@ -25,12 +25,12 @@ import ( ) const ( - clusterSetController = "k3k-clusterset-controller" + clusterPolicyController = "k3k-clusterpolicy-controller" allTrafficCIDR = "0.0.0.0/0" maxConcurrentReconciles = 1 ) -type ClusterSetReconciler struct { +type VirtualClusterPolicyReconciler struct { Client client.Client Scheme *runtime.Scheme ClusterCIDR string @@ -39,14 +39,14 @@ type ClusterSetReconciler struct { // Add adds a new controller to the manager func Add(ctx context.Context, mgr manager.Manager, clusterCIDR string) error { // initialize a new Reconciler - reconciler := ClusterSetReconciler{ + reconciler := VirtualClusterPolicyReconciler{ Client: mgr.GetClient(), Scheme: mgr.GetScheme(), ClusterCIDR: clusterCIDR, } return ctrl.NewControllerManagedBy(mgr). - For(&v1alpha1.ClusterSet{}). + For(&v1alpha1.VirtualClusterPolicy{}). Owns(&networkingv1.NetworkPolicy{}). Owns(&v1.ResourceQuota{}). WithOptions(controller.Options{ @@ -64,8 +64,8 @@ func Add(ctx context.Context, mgr manager.Manager, clusterCIDR string) error { Complete(&reconciler) } -// namespaceEventHandler will enqueue a reconcile request for the ClusterSet in the given namespace -func namespaceEventHandler(reconciler ClusterSetReconciler) handler.MapFunc { +// namespaceEventHandler will enqueue a reconcile request for the VirtualClusterPolicy in the given namespace +func namespaceEventHandler(reconciler VirtualClusterPolicyReconciler) handler.MapFunc { return func(ctx context.Context, obj client.Object) []reconcile.Request { // if the object is a Namespace, use the name as the namespace namespace := obj.GetName() @@ -80,8 +80,8 @@ func namespaceEventHandler(reconciler ClusterSetReconciler) handler.MapFunc { Namespace: namespace, } - var clusterSet v1alpha1.ClusterSet - if err := reconciler.Client.Get(ctx, key, &clusterSet); err != nil { + var policy v1alpha1.VirtualClusterPolicy + if err := reconciler.Client.Get(ctx, key, &policy); err != nil { return nil } @@ -101,22 +101,22 @@ func namespaceLabelsPredicate() predicate.Predicate { } } -func (c *ClusterSetReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { - log := ctrl.LoggerFrom(ctx).WithValues("clusterset", req.NamespacedName) +func (c *VirtualClusterPolicyReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { + log := ctrl.LoggerFrom(ctx).WithValues("clusterpolicy", req.NamespacedName) ctx = ctrl.LoggerInto(ctx, log) // enrich the current logger - var clusterSet v1alpha1.ClusterSet - if err := c.Client.Get(ctx, req.NamespacedName, &clusterSet); err != nil { + var policy v1alpha1.VirtualClusterPolicy + if err := c.Client.Get(ctx, req.NamespacedName, &policy); err != nil { return reconcile.Result{}, client.IgnoreNotFound(err) } - orig := clusterSet.DeepCopy() + orig := policy.DeepCopy() - reconcilerErr := c.reconcileClusterSet(ctx, &clusterSet) + reconcilerErr := c.reconcileVirtualClusterPolicy(ctx, &policy) // update Status if needed - if !reflect.DeepEqual(orig.Status, clusterSet.Status) { - if err := c.Client.Status().Update(ctx, &clusterSet); err != nil { + if !reflect.DeepEqual(orig.Status, policy.Status) { + if err := c.Client.Status().Update(ctx, &policy); err != nil { return reconcile.Result{}, err } } @@ -126,9 +126,9 @@ func (c *ClusterSetReconciler) Reconcile(ctx context.Context, req reconcile.Requ return reconcile.Result{}, reconcilerErr } - // update ClusterSet if needed - if !reflect.DeepEqual(orig.Spec, clusterSet.Spec) { - if err := c.Client.Update(ctx, &clusterSet); err != nil { + // update VirtualClusterPolicy if needed + if !reflect.DeepEqual(orig.Spec, policy.Spec) { + if err := c.Client.Update(ctx, &policy); err != nil { return reconcile.Result{}, err } } @@ -136,45 +136,45 @@ func (c *ClusterSetReconciler) Reconcile(ctx context.Context, req reconcile.Requ return reconcile.Result{}, nil } -func (c *ClusterSetReconciler) reconcileClusterSet(ctx context.Context, clusterSet *v1alpha1.ClusterSet) error { - if err := c.reconcileNetworkPolicy(ctx, clusterSet); err != nil { +func (c *VirtualClusterPolicyReconciler) reconcileVirtualClusterPolicy(ctx context.Context, policy *v1alpha1.VirtualClusterPolicy) error { + if err := c.reconcileNetworkPolicy(ctx, policy); err != nil { return err } - if err := c.reconcileNamespacePodSecurityLabels(ctx, clusterSet); err != nil { + if err := c.reconcileNamespacePodSecurityLabels(ctx, policy); err != nil { return err } - if err := c.reconcileLimit(ctx, clusterSet); err != nil { + if err := c.reconcileLimit(ctx, policy); err != nil { return err } - if err := c.reconcileQuota(ctx, clusterSet); err != nil { + if err := c.reconcileQuota(ctx, policy); err != nil { return err } - if err := c.reconcileClusters(ctx, clusterSet); err != nil { + if err := c.reconcileClusters(ctx, policy); err != nil { return err } return nil } -func (c *ClusterSetReconciler) reconcileNetworkPolicy(ctx context.Context, clusterSet *v1alpha1.ClusterSet) error { +func (c *VirtualClusterPolicyReconciler) reconcileNetworkPolicy(ctx context.Context, policy *v1alpha1.VirtualClusterPolicy) error { log := ctrl.LoggerFrom(ctx) log.Info("reconciling NetworkPolicy") - networkPolicy, err := netpol(ctx, c.ClusterCIDR, clusterSet, c.Client) + networkPolicy, err := netpol(ctx, c.ClusterCIDR, policy, c.Client) if err != nil { return err } - if err = ctrl.SetControllerReference(clusterSet, networkPolicy, c.Scheme); err != nil { + if err = ctrl.SetControllerReference(policy, networkPolicy, c.Scheme); err != nil { return err } // if disabled then delete the existing network policy - if clusterSet.Spec.DisableNetworkPolicy { + if policy.Spec.DisableNetworkPolicy { err := c.Client.Delete(ctx, networkPolicy) return client.IgnoreNotFound(err) } @@ -188,7 +188,7 @@ func (c *ClusterSetReconciler) reconcileNetworkPolicy(ctx context.Context, clust return err } -func netpol(ctx context.Context, clusterCIDR string, clusterSet *v1alpha1.ClusterSet, client client.Client) (*networkingv1.NetworkPolicy, error) { +func netpol(ctx context.Context, clusterCIDR string, policy *v1alpha1.VirtualClusterPolicy, client client.Client) (*networkingv1.NetworkPolicy, error) { var cidrList []string if clusterCIDR != "" { @@ -206,8 +206,8 @@ func netpol(ctx context.Context, clusterCIDR string, clusterSet *v1alpha1.Cluste return &networkingv1.NetworkPolicy{ ObjectMeta: metav1.ObjectMeta{ - Name: k3kcontroller.SafeConcatNameWithPrefix(clusterSet.Name), - Namespace: clusterSet.Namespace, + Name: k3kcontroller.SafeConcatNameWithPrefix(policy.Name), + Namespace: policy.Namespace, }, TypeMeta: metav1.TypeMeta{ Kind: "NetworkPolicy", @@ -233,7 +233,7 @@ func netpol(ctx context.Context, clusterCIDR string, clusterSet *v1alpha1.Cluste { NamespaceSelector: &metav1.LabelSelector{ MatchLabels: map[string]string{ - "kubernetes.io/metadata.name": clusterSet.Namespace, + "kubernetes.io/metadata.name": policy.Namespace, }, }, }, @@ -256,13 +256,13 @@ func netpol(ctx context.Context, clusterCIDR string, clusterSet *v1alpha1.Cluste }, nil } -func (c *ClusterSetReconciler) reconcileNamespacePodSecurityLabels(ctx context.Context, clusterSet *v1alpha1.ClusterSet) error { +func (c *VirtualClusterPolicyReconciler) reconcileNamespacePodSecurityLabels(ctx context.Context, policy *v1alpha1.VirtualClusterPolicy) error { log := ctrl.LoggerFrom(ctx) log.Info("reconciling Namespace") var ns v1.Namespace - key := types.NamespacedName{Name: clusterSet.Namespace} + key := types.NamespacedName{Name: policy.Namespace} if err := c.Client.Get(ctx, key, &ns); err != nil { return err } @@ -279,8 +279,8 @@ func (c *ClusterSetReconciler) reconcileNamespacePodSecurityLabels(ctx context.C delete(newLabels, "pod-security.kubernetes.io/warn-version") // if a PSA level is specified add the proper labels - if clusterSet.Spec.PodSecurityAdmissionLevel != nil { - psaLevel := *clusterSet.Spec.PodSecurityAdmissionLevel + if policy.Spec.PodSecurityAdmissionLevel != nil { + psaLevel := *policy.Spec.PodSecurityAdmissionLevel newLabels["pod-security.kubernetes.io/enforce"] = string(psaLevel) newLabels["pod-security.kubernetes.io/enforce-version"] = "latest" @@ -303,12 +303,12 @@ func (c *ClusterSetReconciler) reconcileNamespacePodSecurityLabels(ctx context.C return nil } -func (c *ClusterSetReconciler) reconcileClusters(ctx context.Context, clusterSet *v1alpha1.ClusterSet) error { +func (c *VirtualClusterPolicyReconciler) reconcileClusters(ctx context.Context, policy *v1alpha1.VirtualClusterPolicy) error { log := ctrl.LoggerFrom(ctx) log.Info("reconciling Clusters") var clusters v1alpha1.ClusterList - if err := c.Client.List(ctx, &clusters, client.InNamespace(clusterSet.Namespace)); err != nil { + if err := c.Client.List(ctx, &clusters, client.InNamespace(policy.Namespace)); err != nil { return err } @@ -317,12 +317,12 @@ func (c *ClusterSetReconciler) reconcileClusters(ctx context.Context, clusterSet for _, cluster := range clusters.Items { oldClusterSpec := cluster.Spec - if cluster.Spec.PriorityClass != clusterSet.Spec.DefaultPriorityClass { - cluster.Spec.PriorityClass = clusterSet.Spec.DefaultPriorityClass + if cluster.Spec.PriorityClass != policy.Spec.DefaultPriorityClass { + cluster.Spec.PriorityClass = policy.Spec.DefaultPriorityClass } - if !reflect.DeepEqual(cluster.Spec.NodeSelector, clusterSet.Spec.DefaultNodeSelector) { - cluster.Spec.NodeSelector = clusterSet.Spec.DefaultNodeSelector + if !reflect.DeepEqual(cluster.Spec.NodeSelector, policy.Spec.DefaultNodeSelector) { + cluster.Spec.NodeSelector = policy.Spec.DefaultNodeSelector } if !reflect.DeepEqual(oldClusterSpec, cluster.Spec) { @@ -334,14 +334,14 @@ func (c *ClusterSetReconciler) reconcileClusters(ctx context.Context, clusterSet return err } -func (c *ClusterSetReconciler) reconcileQuota(ctx context.Context, clusterSet *v1alpha1.ClusterSet) error { - if clusterSet.Spec.Quota == nil { +func (c *VirtualClusterPolicyReconciler) reconcileQuota(ctx context.Context, policy *v1alpha1.VirtualClusterPolicy) error { + if policy.Spec.Quota == nil { // check if resourceQuota object exists and deletes it. var toDeleteResourceQuota v1.ResourceQuota key := types.NamespacedName{ - Name: k3kcontroller.SafeConcatNameWithPrefix(clusterSet.Name), - Namespace: clusterSet.Namespace, + Name: k3kcontroller.SafeConcatNameWithPrefix(policy.Name), + Namespace: policy.Namespace, } if err := c.Client.Get(ctx, key, &toDeleteResourceQuota); err != nil { @@ -352,9 +352,9 @@ func (c *ClusterSetReconciler) reconcileQuota(ctx context.Context, clusterSet *v } // create/update resource Quota - resourceQuota := resourceQuota(clusterSet) + resourceQuota := resourceQuota(policy) - if err := ctrl.SetControllerReference(clusterSet, &resourceQuota, c.Scheme); err != nil { + if err := ctrl.SetControllerReference(policy, &resourceQuota, c.Scheme); err != nil { return err } @@ -367,31 +367,31 @@ func (c *ClusterSetReconciler) reconcileQuota(ctx context.Context, clusterSet *v return nil } -func resourceQuota(clusterSet *v1alpha1.ClusterSet) v1.ResourceQuota { +func resourceQuota(policy *v1alpha1.VirtualClusterPolicy) v1.ResourceQuota { return v1.ResourceQuota{ ObjectMeta: metav1.ObjectMeta{ - Name: k3kcontroller.SafeConcatNameWithPrefix(clusterSet.Name), - Namespace: clusterSet.Namespace, + Name: k3kcontroller.SafeConcatNameWithPrefix(policy.Name), + Namespace: policy.Namespace, }, TypeMeta: metav1.TypeMeta{ Kind: "ResourceQuota", APIVersion: "v1", }, - Spec: *clusterSet.Spec.Quota, + Spec: *policy.Spec.Quota, } } -func (c *ClusterSetReconciler) reconcileLimit(ctx context.Context, clusterSet *v1alpha1.ClusterSet) error { +func (c *VirtualClusterPolicyReconciler) reconcileLimit(ctx context.Context, policy *v1alpha1.VirtualClusterPolicy) error { log := ctrl.LoggerFrom(ctx) - log.Info("Reconciling ClusterSet Limit") + log.Info("Reconciling VirtualClusterPolicy Limit") // delete limitrange if spec.limits isnt specified. - if clusterSet.Spec.Limit == nil { + if policy.Spec.Limit == nil { var toDeleteLimitRange v1.LimitRange key := types.NamespacedName{ - Name: k3kcontroller.SafeConcatNameWithPrefix(clusterSet.Name), - Namespace: clusterSet.Namespace, + Name: k3kcontroller.SafeConcatNameWithPrefix(policy.Name), + Namespace: policy.Namespace, } if err := c.Client.Get(ctx, key, &toDeleteLimitRange); err != nil { @@ -401,8 +401,8 @@ func (c *ClusterSetReconciler) reconcileLimit(ctx context.Context, clusterSet *v return c.Client.Delete(ctx, &toDeleteLimitRange) } - limitRange := limitRange(clusterSet) - if err := ctrl.SetControllerReference(clusterSet, &limitRange, c.Scheme); err != nil { + limitRange := limitRange(policy) + if err := ctrl.SetControllerReference(policy, &limitRange, c.Scheme); err != nil { return err } @@ -415,16 +415,16 @@ func (c *ClusterSetReconciler) reconcileLimit(ctx context.Context, clusterSet *v return nil } -func limitRange(clusterSet *v1alpha1.ClusterSet) v1.LimitRange { +func limitRange(policy *v1alpha1.VirtualClusterPolicy) v1.LimitRange { return v1.LimitRange{ ObjectMeta: metav1.ObjectMeta{ - Name: k3kcontroller.SafeConcatNameWithPrefix(clusterSet.Name), - Namespace: clusterSet.Namespace, + Name: k3kcontroller.SafeConcatNameWithPrefix(policy.Name), + Namespace: policy.Namespace, }, TypeMeta: metav1.TypeMeta{ Kind: "LimitRange", APIVersion: "v1", }, - Spec: *clusterSet.Spec.Limit, + Spec: *policy.Spec.Limit, } } diff --git a/pkg/controller/clusterset/clusterset_suite_test.go b/pkg/controller/policy/policy_suite_test.go similarity index 92% rename from pkg/controller/clusterset/clusterset_suite_test.go rename to pkg/controller/policy/policy_suite_test.go index 466c6c9c..dbc7a070 100644 --- a/pkg/controller/clusterset/clusterset_suite_test.go +++ b/pkg/controller/policy/policy_suite_test.go @@ -1,4 +1,4 @@ -package clusterset_test +package policy_test import ( "context" @@ -7,7 +7,7 @@ import ( "github.com/go-logr/zapr" "github.com/rancher/k3k/pkg/apis/k3k.io/v1alpha1" - "github.com/rancher/k3k/pkg/controller/clusterset" + "github.com/rancher/k3k/pkg/controller/policy" "go.uber.org/zap" appsv1 "k8s.io/api/apps/v1" @@ -24,7 +24,7 @@ import ( func TestController(t *testing.T) { RegisterFailHandler(Fail) - RunSpecs(t, "ClusterSet Controller Suite") + RunSpecs(t, "VirtualClusterPolicy Controller Suite") } var ( @@ -54,7 +54,7 @@ var _ = BeforeSuite(func() { ctrl.SetLogger(zapr.NewLogger(zap.NewNop())) ctx, cancel = context.WithCancel(context.Background()) - err = clusterset.Add(ctx, mgr, "") + err = policy.Add(ctx, mgr, "") Expect(err).NotTo(HaveOccurred()) go func() { diff --git a/pkg/controller/clusterset/clusterset_test.go b/pkg/controller/policy/policy_test.go similarity index 79% rename from pkg/controller/clusterset/clusterset_test.go rename to pkg/controller/policy/policy_test.go index 3b44faf6..564861fb 100644 --- a/pkg/controller/clusterset/clusterset_test.go +++ b/pkg/controller/policy/policy_test.go @@ -1,4 +1,4 @@ -package clusterset_test +package policy_test import ( "context" @@ -20,9 +20,9 @@ import ( . "github.com/onsi/gomega" ) -var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet"), func() { +var _ = Describe("VirtualClusterPolicy Controller", Label("controller"), Label("VirtualClusterPolicy"), func() { - Context("creating a ClusterSet", func() { + Context("creating a VirtualClusterPolicy", func() { var ( namespace string @@ -37,23 +37,23 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet When("created with a default spec", func() { It("should have only the 'shared' allowedModeTypes", func() { - clusterSet := &v1alpha1.ClusterSet{ + policy := &v1alpha1.VirtualClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ Name: "default", Namespace: namespace, }, } - err := k8sClient.Create(ctx, clusterSet) + err := k8sClient.Create(ctx, policy) Expect(err).To(Not(HaveOccurred())) - allowedModeTypes := clusterSet.Spec.AllowedModeTypes + allowedModeTypes := policy.Spec.AllowedModeTypes Expect(allowedModeTypes).To(HaveLen(1)) Expect(allowedModeTypes).To(ContainElement(v1alpha1.SharedClusterMode)) }) It("should not be able to create a cluster with a non 'default' name", func() { - err := k8sClient.Create(ctx, &v1alpha1.ClusterSet{ + err := k8sClient.Create(ctx, &v1alpha1.VirtualClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ Name: "another-name", Namespace: namespace, @@ -62,8 +62,8 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet Expect(err).To(HaveOccurred()) }) - It("should not be able to create two ClusterSets in the same namespace", func() { - err := k8sClient.Create(ctx, &v1alpha1.ClusterSet{ + It("should not be able to create two VirtualClusterPolicys in the same namespace", func() { + err := k8sClient.Create(ctx, &v1alpha1.VirtualClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ Name: "default", Namespace: namespace, @@ -71,7 +71,7 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet }) Expect(err).To(Not(HaveOccurred())) - err = k8sClient.Create(ctx, &v1alpha1.ClusterSet{ + err = k8sClient.Create(ctx, &v1alpha1.VirtualClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ Name: "default-2", Namespace: namespace, @@ -81,31 +81,31 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet }) It("should create a NetworkPolicy", func() { - clusterSet := &v1alpha1.ClusterSet{ + policy := &v1alpha1.VirtualClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ Name: "default", Namespace: namespace, }, } - err := k8sClient.Create(ctx, clusterSet) + err := k8sClient.Create(ctx, policy) Expect(err).To(Not(HaveOccurred())) // look for network policies etc - clusterSetNetworkPolicy := &networkingv1.NetworkPolicy{} + networkPolicy := &networkingv1.NetworkPolicy{} Eventually(func() error { key := types.NamespacedName{ - Name: k3kcontroller.SafeConcatNameWithPrefix(clusterSet.Name), + Name: k3kcontroller.SafeConcatNameWithPrefix(policy.Name), Namespace: namespace, } - return k8sClient.Get(ctx, key, clusterSetNetworkPolicy) + return k8sClient.Get(ctx, key, networkPolicy) }). WithTimeout(time.Minute). WithPolling(time.Second). Should(BeNil()) - spec := clusterSetNetworkPolicy.Spec + spec := networkPolicy.Spec Expect(spec.PolicyTypes).To(ContainElement(networkingv1.PolicyTypeEgress)) Expect(spec.PolicyTypes).To(ContainElement(networkingv1.PolicyTypeIngress)) @@ -121,7 +121,7 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet } // allow networking in the same namespace - clusterSetNamespaceRule := networkingv1.NetworkPolicyPeer{ + namespaceRule := networkingv1.NetworkPolicyPeer{ NamespaceSelector: &metav1.LabelSelector{ MatchLabels: map[string]string{"kubernetes.io/metadata.name": namespace}, }, @@ -138,30 +138,30 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet } Expect(spec.Egress[0].To).To(ContainElements( - ipBlockRule, clusterSetNamespaceRule, kubeDNSRule, + ipBlockRule, namespaceRule, kubeDNSRule, )) }) }) When("created with DisableNetworkPolicy", func() { It("should not create a NetworkPolicy if true", func() { - clusterSet := &v1alpha1.ClusterSet{ + policy := &v1alpha1.VirtualClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ Name: "default", Namespace: namespace, }, - Spec: v1alpha1.ClusterSetSpec{ + Spec: v1alpha1.VirtualClusterPolicySpec{ DisableNetworkPolicy: true, }, } - err := k8sClient.Create(ctx, clusterSet) + err := k8sClient.Create(ctx, policy) Expect(err).To(Not(HaveOccurred())) // wait for a bit for the network policy, but it should not be created Eventually(func() bool { key := types.NamespacedName{ - Name: k3kcontroller.SafeConcatNameWithPrefix(clusterSet.Name), + Name: k3kcontroller.SafeConcatNameWithPrefix(policy.Name), Namespace: namespace, } err := k8sClient.Get(ctx, key, &networkingv1.NetworkPolicy{}) @@ -174,41 +174,41 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet }) It("should delete the NetworkPolicy if changed to false", func() { - clusterSet := &v1alpha1.ClusterSet{ + policy := &v1alpha1.VirtualClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ Name: "default", Namespace: namespace, }, } - err := k8sClient.Create(ctx, clusterSet) + err := k8sClient.Create(ctx, policy) Expect(err).To(Not(HaveOccurred())) // look for network policy - clusterSetNetworkPolicy := &networkingv1.NetworkPolicy{} + networkPolicy := &networkingv1.NetworkPolicy{} Eventually(func() error { key := types.NamespacedName{ - Name: k3kcontroller.SafeConcatNameWithPrefix(clusterSet.Name), + Name: k3kcontroller.SafeConcatNameWithPrefix(policy.Name), Namespace: namespace, } - return k8sClient.Get(ctx, key, clusterSetNetworkPolicy) + return k8sClient.Get(ctx, key, networkPolicy) }). WithTimeout(time.Minute). WithPolling(time.Second). Should(BeNil()) - clusterSet.Spec.DisableNetworkPolicy = true - err = k8sClient.Update(ctx, clusterSet) + policy.Spec.DisableNetworkPolicy = true + err = k8sClient.Update(ctx, policy) Expect(err).To(Not(HaveOccurred())) // wait for a bit for the network policy to being deleted Eventually(func() bool { key := types.NamespacedName{ - Name: k3kcontroller.SafeConcatNameWithPrefix(clusterSet.Name), + Name: k3kcontroller.SafeConcatNameWithPrefix(policy.Name), Namespace: namespace, } - err := k8sClient.Get(ctx, key, clusterSetNetworkPolicy) + err := k8sClient.Get(ctx, key, networkPolicy) return apierrors.IsNotFound(err) }). MustPassRepeatedly(5). @@ -218,47 +218,47 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet }) It("should recreate the NetworkPolicy if deleted", func() { - clusterSet := &v1alpha1.ClusterSet{ + policy := &v1alpha1.VirtualClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ Name: "default", Namespace: namespace, }, } - err := k8sClient.Create(ctx, clusterSet) + err := k8sClient.Create(ctx, policy) Expect(err).To(Not(HaveOccurred())) // look for network policy - clusterSetNetworkPolicy := &networkingv1.NetworkPolicy{} + networkPolicy := &networkingv1.NetworkPolicy{} Eventually(func() error { key := types.NamespacedName{ - Name: k3kcontroller.SafeConcatNameWithPrefix(clusterSet.Name), + Name: k3kcontroller.SafeConcatNameWithPrefix(policy.Name), Namespace: namespace, } - return k8sClient.Get(context.Background(), key, clusterSetNetworkPolicy) + return k8sClient.Get(context.Background(), key, networkPolicy) }). WithTimeout(time.Minute). WithPolling(time.Second). Should(BeNil()) - err = k8sClient.Delete(ctx, clusterSetNetworkPolicy) + err = k8sClient.Delete(ctx, networkPolicy) Expect(err).To(Not(HaveOccurred())) key := types.NamespacedName{ - Name: k3kcontroller.SafeConcatNameWithPrefix(clusterSet.Name), + Name: k3kcontroller.SafeConcatNameWithPrefix(policy.Name), Namespace: namespace, } - err = k8sClient.Get(ctx, key, clusterSetNetworkPolicy) + err = k8sClient.Get(ctx, key, networkPolicy) Expect(apierrors.IsNotFound(err)).Should(BeTrue()) // wait a bit for the network policy to being recreated Eventually(func() error { key := types.NamespacedName{ - Name: k3kcontroller.SafeConcatNameWithPrefix(clusterSet.Name), + Name: k3kcontroller.SafeConcatNameWithPrefix(policy.Name), Namespace: namespace, } - return k8sClient.Get(ctx, key, clusterSetNetworkPolicy) + return k8sClient.Get(ctx, key, networkPolicy) }). WithTimeout(time.Second * 10). WithPolling(time.Second). @@ -269,33 +269,33 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet When("created specifying the mode", func() { It("should have the 'virtual' mode if specified", func() { - clusterSet := &v1alpha1.ClusterSet{ + policy := &v1alpha1.VirtualClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ Name: "default", Namespace: namespace, }, - Spec: v1alpha1.ClusterSetSpec{ + Spec: v1alpha1.VirtualClusterPolicySpec{ AllowedModeTypes: []v1alpha1.ClusterMode{ v1alpha1.VirtualClusterMode, }, }, } - err := k8sClient.Create(ctx, clusterSet) + err := k8sClient.Create(ctx, policy) Expect(err).To(Not(HaveOccurred())) - allowedModeTypes := clusterSet.Spec.AllowedModeTypes + allowedModeTypes := policy.Spec.AllowedModeTypes Expect(allowedModeTypes).To(HaveLen(1)) Expect(allowedModeTypes).To(ContainElement(v1alpha1.VirtualClusterMode)) }) It("should have both modes if specified", func() { - clusterSet := &v1alpha1.ClusterSet{ + policy := &v1alpha1.VirtualClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ Name: "default", Namespace: namespace, }, - Spec: v1alpha1.ClusterSetSpec{ + Spec: v1alpha1.VirtualClusterPolicySpec{ AllowedModeTypes: []v1alpha1.ClusterMode{ v1alpha1.SharedClusterMode, v1alpha1.VirtualClusterMode, @@ -303,10 +303,10 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet }, } - err := k8sClient.Create(ctx, clusterSet) + err := k8sClient.Create(ctx, policy) Expect(err).To(Not(HaveOccurred())) - allowedModeTypes := clusterSet.Spec.AllowedModeTypes + allowedModeTypes := policy.Spec.AllowedModeTypes Expect(allowedModeTypes).To(HaveLen(2)) Expect(allowedModeTypes).To(ContainElements( v1alpha1.SharedClusterMode, @@ -315,12 +315,12 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet }) It("should fail for a non-existing mode", func() { - clusterSet := &v1alpha1.ClusterSet{ + policy := &v1alpha1.VirtualClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ Name: "default", Namespace: namespace, }, - Spec: v1alpha1.ClusterSetSpec{ + Spec: v1alpha1.VirtualClusterPolicySpec{ AllowedModeTypes: []v1alpha1.ClusterMode{ v1alpha1.SharedClusterMode, v1alpha1.VirtualClusterMode, @@ -329,7 +329,7 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet }, } - err := k8sClient.Create(ctx, clusterSet) + err := k8sClient.Create(ctx, policy) Expect(err).To(HaveOccurred()) }) }) @@ -342,17 +342,17 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet restricted = v1alpha1.RestrictedPodSecurityAdmissionLevel ) - clusterSet := &v1alpha1.ClusterSet{ + policy := &v1alpha1.VirtualClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ Name: "default", Namespace: namespace, }, - Spec: v1alpha1.ClusterSetSpec{ + Spec: v1alpha1.VirtualClusterPolicySpec{ PodSecurityAdmissionLevel: &privileged, }, } - err := k8sClient.Create(ctx, clusterSet) + err := k8sClient.Create(ctx, policy) Expect(err).To(Not(HaveOccurred())) var ns v1.Namespace @@ -377,8 +377,8 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet // Check baseline - clusterSet.Spec.PodSecurityAdmissionLevel = &baseline - err = k8sClient.Update(ctx, clusterSet) + policy.Spec.PodSecurityAdmissionLevel = &baseline + err = k8sClient.Update(ctx, policy) Expect(err).To(Not(HaveOccurred())) // wait a bit for the namespace to be updated @@ -399,8 +399,8 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet // Check restricted - clusterSet.Spec.PodSecurityAdmissionLevel = &restricted - err = k8sClient.Update(ctx, clusterSet) + policy.Spec.PodSecurityAdmissionLevel = &restricted + err = k8sClient.Update(ctx, policy) Expect(err).To(Not(HaveOccurred())) // wait a bit for the namespace to be updated @@ -421,8 +421,8 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet // check cleanup - clusterSet.Spec.PodSecurityAdmissionLevel = nil - err = k8sClient.Update(ctx, clusterSet) + policy.Spec.PodSecurityAdmissionLevel = nil + err = k8sClient.Update(ctx, policy) Expect(err).To(Not(HaveOccurred())) // wait a bit for the namespace to be updated @@ -445,17 +445,17 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet It("should restore the labels if Namespace is updated", func() { privileged := v1alpha1.PrivilegedPodSecurityAdmissionLevel - clusterSet := &v1alpha1.ClusterSet{ + policy := &v1alpha1.VirtualClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ Name: "default", Namespace: namespace, }, - Spec: v1alpha1.ClusterSetSpec{ + Spec: v1alpha1.VirtualClusterPolicySpec{ PodSecurityAdmissionLevel: &privileged, }, } - err := k8sClient.Create(ctx, clusterSet) + err := k8sClient.Create(ctx, policy) Expect(err).To(Not(HaveOccurred())) var ns v1.Namespace @@ -496,17 +496,17 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet When("a cluster in the same namespace is present", func() { It("should update it if needed", func() { - clusterSet := &v1alpha1.ClusterSet{ + policy := &v1alpha1.VirtualClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ Name: "default", Namespace: namespace, }, - Spec: v1alpha1.ClusterSetSpec{ + Spec: v1alpha1.VirtualClusterPolicySpec{ DefaultPriorityClass: "foobar", }, } - err := k8sClient.Create(ctx, clusterSet) + err := k8sClient.Create(ctx, policy) Expect(err).To(Not(HaveOccurred())) cluster := &v1alpha1.Cluster{ @@ -529,7 +529,7 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet key := types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace} err = k8sClient.Get(ctx, key, cluster) Expect(err).To(Not(HaveOccurred())) - return cluster.Spec.PriorityClass == clusterSet.Spec.DefaultPriorityClass + return cluster.Spec.PriorityClass == policy.Spec.DefaultPriorityClass }). WithTimeout(time.Second * 10). WithPolling(time.Second). @@ -537,17 +537,17 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet }) It("should update the nodeSelector", func() { - clusterSet := &v1alpha1.ClusterSet{ + policy := &v1alpha1.VirtualClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ Name: "default", Namespace: namespace, }, - Spec: v1alpha1.ClusterSetSpec{ + Spec: v1alpha1.VirtualClusterPolicySpec{ DefaultNodeSelector: map[string]string{"label-1": "value-1"}, }, } - err := k8sClient.Create(ctx, clusterSet) + err := k8sClient.Create(ctx, policy) Expect(err).To(Not(HaveOccurred())) cluster := &v1alpha1.Cluster{ @@ -570,7 +570,7 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet key := types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace} err = k8sClient.Get(ctx, key, cluster) Expect(err).To(Not(HaveOccurred())) - return reflect.DeepEqual(cluster.Spec.NodeSelector, clusterSet.Spec.DefaultNodeSelector) + return reflect.DeepEqual(cluster.Spec.NodeSelector, policy.Spec.DefaultNodeSelector) }). WithTimeout(time.Second * 10). WithPolling(time.Second). @@ -578,17 +578,17 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet }) It("should update the nodeSelector if changed", func() { - clusterSet := &v1alpha1.ClusterSet{ + policy := &v1alpha1.VirtualClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ Name: "default", Namespace: namespace, }, - Spec: v1alpha1.ClusterSetSpec{ + Spec: v1alpha1.VirtualClusterPolicySpec{ DefaultNodeSelector: map[string]string{"label-1": "value-1"}, }, } - err := k8sClient.Create(ctx, clusterSet) + err := k8sClient.Create(ctx, policy) Expect(err).To(Not(HaveOccurred())) cluster := &v1alpha1.Cluster{ @@ -607,20 +607,20 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet err = k8sClient.Create(ctx, cluster) Expect(err).To(Not(HaveOccurred())) - Expect(cluster.Spec.NodeSelector).To(Equal(clusterSet.Spec.DefaultNodeSelector)) + Expect(cluster.Spec.NodeSelector).To(Equal(policy.Spec.DefaultNodeSelector)) - // update the ClusterSet - clusterSet.Spec.DefaultNodeSelector["label-2"] = "value-2" - err = k8sClient.Update(ctx, clusterSet) + // update the VirtualClusterPolicy + policy.Spec.DefaultNodeSelector["label-2"] = "value-2" + err = k8sClient.Update(ctx, policy) Expect(err).To(Not(HaveOccurred())) - Expect(cluster.Spec.NodeSelector).To(Not(Equal(clusterSet.Spec.DefaultNodeSelector))) + Expect(cluster.Spec.NodeSelector).To(Not(Equal(policy.Spec.DefaultNodeSelector))) // wait a bit Eventually(func() bool { key := types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace} err = k8sClient.Get(ctx, key, cluster) Expect(err).To(Not(HaveOccurred())) - return reflect.DeepEqual(cluster.Spec.NodeSelector, clusterSet.Spec.DefaultNodeSelector) + return reflect.DeepEqual(cluster.Spec.NodeSelector, policy.Spec.DefaultNodeSelector) }). WithTimeout(time.Second * 10). WithPolling(time.Second). @@ -630,7 +630,7 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet cluster.Spec.NodeSelector["label-3"] = "value-3" err = k8sClient.Update(ctx, cluster) Expect(err).To(Not(HaveOccurred())) - Expect(cluster.Spec.NodeSelector).To(Not(Equal(clusterSet.Spec.DefaultNodeSelector))) + Expect(cluster.Spec.NodeSelector).To(Not(Equal(policy.Spec.DefaultNodeSelector))) // wait a bit and check it's restored Eventually(func() bool { @@ -639,7 +639,7 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet key := types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace} err = k8sClient.Get(ctx, key, &updatedCluster) Expect(err).To(Not(HaveOccurred())) - return reflect.DeepEqual(updatedCluster.Spec.NodeSelector, clusterSet.Spec.DefaultNodeSelector) + return reflect.DeepEqual(updatedCluster.Spec.NodeSelector, policy.Spec.DefaultNodeSelector) }). WithTimeout(time.Second * 10). WithPolling(time.Second). @@ -649,17 +649,17 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet When("a cluster in a different namespace is present", func() { It("should not be update", func() { - clusterSet := &v1alpha1.ClusterSet{ + policy := &v1alpha1.VirtualClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ Name: "default", Namespace: namespace, }, - Spec: v1alpha1.ClusterSetSpec{ + Spec: v1alpha1.VirtualClusterPolicySpec{ DefaultPriorityClass: "foobar", }, } - err := k8sClient.Create(ctx, clusterSet) + err := k8sClient.Create(ctx, policy) Expect(err).To(Not(HaveOccurred())) namespace2 := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{GenerateName: "ns-"}} @@ -686,7 +686,7 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet key := types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace} err = k8sClient.Get(ctx, key, cluster) Expect(err).To(Not(HaveOccurred())) - return cluster.Spec.PriorityClass != clusterSet.Spec.DefaultPriorityClass + return cluster.Spec.PriorityClass != policy.Spec.DefaultPriorityClass }). MustPassRepeatedly(5). WithTimeout(time.Second * 10). @@ -697,12 +697,12 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet When("created with ResourceQuota", func() { It("should create resourceQuota if Quota is enabled", func() { - clusterSet := &v1alpha1.ClusterSet{ + policy := &v1alpha1.VirtualClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ Name: "default", Namespace: namespace, }, - Spec: v1alpha1.ClusterSetSpec{ + Spec: v1alpha1.VirtualClusterPolicySpec{ Quota: &v1.ResourceQuotaSpec{ Hard: v1.ResourceList{ v1.ResourceCPU: resource.MustParse("800m"), @@ -712,13 +712,13 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet }, } - err := k8sClient.Create(ctx, clusterSet) + err := k8sClient.Create(ctx, policy) Expect(err).To(Not(HaveOccurred())) var resourceQuota v1.ResourceQuota Eventually(func() error { key := types.NamespacedName{ - Name: k3kcontroller.SafeConcatNameWithPrefix(clusterSet.Name), + Name: k3kcontroller.SafeConcatNameWithPrefix(policy.Name), Namespace: namespace, } @@ -732,12 +732,12 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet }) It("should delete the ResourceQuota if Quota is deleted", func() { - clusterSet := &v1alpha1.ClusterSet{ + policy := &v1alpha1.VirtualClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ Name: "default", Namespace: namespace, }, - Spec: v1alpha1.ClusterSetSpec{ + Spec: v1alpha1.VirtualClusterPolicySpec{ Quota: &v1.ResourceQuotaSpec{ Hard: v1.ResourceList{ v1.ResourceCPU: resource.MustParse("800m"), @@ -747,14 +747,14 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet }, } - err := k8sClient.Create(ctx, clusterSet) + err := k8sClient.Create(ctx, policy) Expect(err).To(Not(HaveOccurred())) var resourceQuota v1.ResourceQuota Eventually(func() error { key := types.NamespacedName{ - Name: k3kcontroller.SafeConcatNameWithPrefix(clusterSet.Name), + Name: k3kcontroller.SafeConcatNameWithPrefix(policy.Name), Namespace: namespace, } return k8sClient.Get(ctx, key, &resourceQuota) @@ -763,14 +763,14 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet WithPolling(time.Second). Should(BeNil()) - clusterSet.Spec.Quota = nil - err = k8sClient.Update(ctx, clusterSet) + policy.Spec.Quota = nil + err = k8sClient.Update(ctx, policy) Expect(err).To(Not(HaveOccurred())) // wait for a bit for the resourceQuota to be deleted Eventually(func() bool { key := types.NamespacedName{ - Name: k3kcontroller.SafeConcatNameWithPrefix(clusterSet.Name), + Name: k3kcontroller.SafeConcatNameWithPrefix(policy.Name), Namespace: namespace, } err := k8sClient.Get(ctx, key, &resourceQuota) @@ -782,12 +782,12 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet }) It("should create resourceQuota if Quota is enabled", func() { - clusterSet := &v1alpha1.ClusterSet{ + policy := &v1alpha1.VirtualClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ Name: "default", Namespace: namespace, }, - Spec: v1alpha1.ClusterSetSpec{ + Spec: v1alpha1.VirtualClusterPolicySpec{ Limit: &v1.LimitRangeSpec{ Limits: []v1.LimitRangeItem{ { @@ -801,14 +801,14 @@ var _ = Describe("ClusterSet Controller", Label("controller"), Label("ClusterSet }, } - err := k8sClient.Create(ctx, clusterSet) + err := k8sClient.Create(ctx, policy) Expect(err).To(Not(HaveOccurred())) var limitRange v1.LimitRange Eventually(func() error { key := types.NamespacedName{ - Name: k3kcontroller.SafeConcatNameWithPrefix(clusterSet.Name), + Name: k3kcontroller.SafeConcatNameWithPrefix(policy.Name), Namespace: namespace, } return k8sClient.Get(ctx, key, &limitRange)