diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..2348331 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,26 @@ +name: Golang Unit tests + +on: + push: + branches: [ "*" ] + pull_request: + branches: [ "*" ] + +jobs: + build: + + runs-on: ubuntu-latest + strategy: + matrix: + go-version: [ '1.19', '1.20', '1.21.x' ] + + steps: + - uses: actions/checkout@v4 + - name: Setup Go ${{ matrix.go-version }} + uses: actions/setup-go@v4 + with: + go-version: ${{ matrix.go-version }} + - name: Install dependencies + run: go get . + - name: Run unit tests with Go CLI + run: go test ./... -v \ No newline at end of file diff --git a/.golangci.yml b/.golangci.yml index 69d5ad2..485a852 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -22,6 +22,7 @@ linters-settings: linters: enable-all: true disable: + - depguard - nestif - nonamedreturns - exhaustruct @@ -54,4 +55,5 @@ service: run: skip-files: - - "zz_.*\\.go$" \ No newline at end of file + - "zz_.*\\.go$" + - "_test*.go" \ No newline at end of file diff --git a/Makefile b/Makefile index 6795072..6455fb7 100644 --- a/Makefile +++ b/Makefile @@ -114,7 +114,7 @@ apidoc: apidocs-gen ## Generate CRD Documentation GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint golangci-lint: ## Download golangci-lint locally if necessary. - $(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint@v1.52.2) + $(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint@v1.54.2) # Linting code as PR is expecting .PHONY: golint @@ -193,13 +193,14 @@ undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/confi # Helm SRC_ROOT = $(shell git rev-parse --show-toplevel) +HELM_DOCS = $(shell pwd)/bin/helm-docs +.PHONY: helm-docs-ensure +helm-docs-ensure: ## Download helm-docs locally if necessary. + $(call go-install-tool,$(HELM_DOCS),github.com/norwoodj/helm-docs/cmd/helm-docs@v1.11.0) .PHONY: helm-docs -helm-docs: HELMDOCS_VERSION := v1.11.0 -helm-docs: docker ## Run helm-docs within docker. - @docker run --rm -v "$(SRC_ROOT):/helm-docs" jnorwood/helm-docs:$(HELMDOCS_VERSION) --chart-search-root /helm-docs - - +helm-docs: helm-docs-ensure ## Run helm-docs. + $(HELM_DOCS) --chart-search-root $(shell pwd)/charts .PHONY: helm-lint helm-lint: docker ## Run ct test linter in docker. @@ -255,3 +256,7 @@ $(CONTROLLER_GEN): $(LOCALBIN) envtest: $(ENVTEST) ## Download envtest-setup locally if necessary. $(ENVTEST): $(LOCALBIN) test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest + + +.PHONY: merge-request +merge-request: manifests installer golint apidoc helm-docs ## Run Local checks before a opening merge request diff --git a/PROJECT b/PROJECT index 62d7bed..3b8dcdd 100644 --- a/PROJECT +++ b/PROJECT @@ -1,3 +1,7 @@ +# Code generated by tool. DO NOT EDIT. +# This file is used to track the info used to scaffold your project +# and allow the plugins properly work. +# More info: https://book.kubebuilder.io/reference/project-config.html domain: nauticus.io layout: - go.kubebuilder.io/v3 @@ -13,4 +17,13 @@ resources: kind: Space path: github.com/edixos/nauticus/api/v1alpha1 version: v1alpha1 +- api: + crdVersion: v1 + namespaced: true + controller: true + domain: nauticus.io + group: nauticus.io + kind: SpaceTemplate + path: github.com/edixos/nauticus/api/v1alpha1 + version: v1alpha1 version: "3" diff --git a/api/v1alpha1/space_types.go b/api/v1alpha1/space_types.go index 475445b..1a512b3 100644 --- a/api/v1alpha1/space_types.go +++ b/api/v1alpha1/space_types.go @@ -29,6 +29,18 @@ type SpaceSpec struct { LimitRanges LimitRangesSpec `json:"limitRanges,omitempty"` // Specifies a list of service account to create within the Space. Optional ServiceAccounts ServiceAccountsSpec `json:"serviceAccounts,omitempty"` + // Reference to a SpaceTemplate + TemplateRef SpaceTemplateReference `json:"templateRef,omitempty"` +} + +// SpaceTemplateReference. +type SpaceTemplateReference struct { + // Name of the SpaceTemplate. + Name string `json:"name,omitempty"` + // Kind specifies the kind of the referenced resource, which should be "SpaceTemplate". + Kind string `json:"kind,omitempty"` + // Group is the API group of the SpaceTemplate, "nauticus.io/v1alpha1". + Group string `json:"group,omitempty"` } // SpaceStatus defines the observed state of Space. @@ -55,6 +67,14 @@ type Space struct { Status SpaceStatus `json:"status,omitempty"` } +func (s *Space) GetConditions() []metav1.Condition { + return s.Status.Conditions +} + +func (s *Space) SetConditions(conditions []metav1.Condition) { + s.Status.Conditions = conditions +} + //+kubebuilder:object:root=true // SpaceList contains a list of Space. diff --git a/api/v1alpha1/spacetemplate_types.go b/api/v1alpha1/spacetemplate_types.go new file mode 100644 index 0000000..7cbf54a --- /dev/null +++ b/api/v1alpha1/spacetemplate_types.go @@ -0,0 +1,66 @@ +// Copyright 2022-2023 Edixos +// SPDX-License-Identifier: Apache-2.0 + +package v1alpha1 + +import ( + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! +// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. + +// SpaceTemplateSpec defines the desired state of SpaceTemplate. +type SpaceTemplateSpec struct { + // Specifies a list of ResourceQuota resources assigned to the Space. The assigned values are inherited by the namespace created by the Space. Optional. + ResourceQuota corev1.ResourceQuotaSpec `json:"resourceQuota,omitempty"` + // Specifies additional RoleBindings assigned to the Space. Nauticus will ensure that the namespace in the Space always contain the RoleBinding for the given ClusterRole. Optional. + AdditionalRoleBindings AdditionalRoleBindingsSpec `json:"additionalRoleBindings,omitempty"` + // Specifies the NetworkPolicies assigned to the Tenant. The assigned NetworkPolicies are inherited by the namespace created in the Space. Optional. + NetworkPolicies NetworkPolicies `json:"networkPolicies,omitempty"` + // Specifies the resource min/max usage restrictions to the Space. Optional. + LimitRanges LimitRangesSpec `json:"limitRanges,omitempty"` +} + +// SpaceTemplateStatus defines the observed state of SpaceTemplate. +type SpaceTemplateStatus struct { + // Conditions List of status conditions to indicate the status of Space + Conditions []metav1.Condition `json:"conditions,omitempty"` +} + +// +kubebuilder:object:root=true +// +kubebuilder:resource:scope=Cluster, categories={spacetemplate} +// +kubebuilder:subresource:status +// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Age" +// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].status",description="Ready" + +// SpaceTemplate is the Schema for the spacetemplates API. +type SpaceTemplate struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec SpaceTemplateSpec `json:"spec,omitempty"` + Status SpaceTemplateStatus `json:"status,omitempty"` +} + +func (in *SpaceTemplate) GetConditions() []metav1.Condition { + return in.Status.Conditions +} + +func (in *SpaceTemplate) SetConditions(conditions []metav1.Condition) { + in.Status.Conditions = conditions +} + +//+kubebuilder:object:root=true + +// SpaceTemplateList contains a list of SpaceTemplate. +type SpaceTemplateList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []SpaceTemplate `json:"items"` +} + +func init() { + SchemeBuilder.Register(&SpaceTemplate{}, &SpaceTemplateList{}) +} diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 590fa63..53a2184 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -258,6 +258,7 @@ func (in *SpaceSpec) DeepCopyInto(out *SpaceSpec) { in.NetworkPolicies.DeepCopyInto(&out.NetworkPolicies) in.LimitRanges.DeepCopyInto(&out.LimitRanges) in.ServiceAccounts.DeepCopyInto(&out.ServiceAccounts) + out.TemplateRef = in.TemplateRef } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new SpaceSpec. @@ -291,3 +292,124 @@ func (in *SpaceStatus) DeepCopy() *SpaceStatus { in.DeepCopyInto(out) return out } + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *SpaceTemplate) DeepCopyInto(out *SpaceTemplate) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + in.Spec.DeepCopyInto(&out.Spec) + in.Status.DeepCopyInto(&out.Status) +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new SpaceTemplate. +func (in *SpaceTemplate) DeepCopy() *SpaceTemplate { + if in == nil { + return nil + } + out := new(SpaceTemplate) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *SpaceTemplate) 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 *SpaceTemplateList) DeepCopyInto(out *SpaceTemplateList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]SpaceTemplate, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new SpaceTemplateList. +func (in *SpaceTemplateList) DeepCopy() *SpaceTemplateList { + if in == nil { + return nil + } + out := new(SpaceTemplateList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *SpaceTemplateList) 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 *SpaceTemplateReference) DeepCopyInto(out *SpaceTemplateReference) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new SpaceTemplateReference. +func (in *SpaceTemplateReference) DeepCopy() *SpaceTemplateReference { + if in == nil { + return nil + } + out := new(SpaceTemplateReference) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *SpaceTemplateSpec) DeepCopyInto(out *SpaceTemplateSpec) { + *out = *in + in.ResourceQuota.DeepCopyInto(&out.ResourceQuota) + if in.AdditionalRoleBindings != nil { + in, out := &in.AdditionalRoleBindings, &out.AdditionalRoleBindings + *out = make(AdditionalRoleBindingsSpec, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } + in.NetworkPolicies.DeepCopyInto(&out.NetworkPolicies) + in.LimitRanges.DeepCopyInto(&out.LimitRanges) +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new SpaceTemplateSpec. +func (in *SpaceTemplateSpec) DeepCopy() *SpaceTemplateSpec { + if in == nil { + return nil + } + out := new(SpaceTemplateSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *SpaceTemplateStatus) DeepCopyInto(out *SpaceTemplateStatus) { + *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 SpaceTemplateStatus. +func (in *SpaceTemplateStatus) DeepCopy() *SpaceTemplateStatus { + if in == nil { + return nil + } + out := new(SpaceTemplateStatus) + in.DeepCopyInto(out) + return out +} diff --git a/charts/nauticus/crds/nauticus_crds.yaml b/charts/nauticus/crds/nauticus_crds.yaml index c3a91c9..fab78e8 100644 --- a/charts/nauticus/crds/nauticus_crds.yaml +++ b/charts/nauticus/crds/nauticus_crds.yaml @@ -518,6 +518,19 @@ spec: type: object type: array type: object + templateRef: + description: Reference to a SpaceTemplate + properties: + group: + description: Group is the API group of the SpaceTemplate, "nauticus.io/v1alpha1". + type: string + kind: + description: Kind specifies the kind of the referenced resource, which should be "SpaceTemplate". + type: string + name: + description: Name of the SpaceTemplate. + type: string + type: object type: object status: description: SpaceStatus defines the observed state of Space. @@ -575,3 +588,534 @@ spec: storage: true subresources: status: {} +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.10.0 + creationTimestamp: null + name: spacetemplates.nauticus.io +spec: + group: nauticus.io + names: + categories: + - spacetemplate + kind: SpaceTemplate + listKind: SpaceTemplateList + plural: spacetemplates + singular: spacetemplate + scope: Cluster + versions: + - additionalPrinterColumns: + - description: Age + jsonPath: .metadata.creationTimestamp + name: Age + type: date + - description: Ready + jsonPath: .status.conditions[?(@.type=='Ready')].status + name: Ready + type: string + name: v1alpha1 + schema: + openAPIV3Schema: + description: SpaceTemplate is the Schema for the spacetemplates API. + properties: + apiVersion: + description: 'APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources' + type: string + kind: + description: 'Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' + type: string + metadata: + type: object + spec: + description: SpaceTemplateSpec defines the desired state of SpaceTemplate. + properties: + additionalRoleBindings: + description: Specifies additional RoleBindings assigned to the Space. Nauticus will ensure that the namespace in the Space always contain the RoleBinding for the given ClusterRole. Optional. + items: + properties: + roleRef: + description: RoleRef contains information that points to the role being used + properties: + apiGroup: + description: APIGroup is the group for the resource being referenced + type: string + kind: + description: Kind is the type of resource being referenced + type: string + name: + description: Name is the name of resource being referenced + type: string + required: + - apiGroup + - kind + - name + type: object + x-kubernetes-map-type: atomic + subjects: + items: + description: Subject contains a reference to the object or user identities a role binding applies to. This can either hold a direct API object reference, or a value for non-objects such as user and group names. + properties: + apiGroup: + description: APIGroup holds the API group of the referenced subject. Defaults to "" for ServiceAccount subjects. Defaults to "rbac.authorization.k8s.io" for User and Group subjects. + type: string + kind: + description: Kind of object being referenced. Values defined by this API group are "User", "Group", and "ServiceAccount". If the Authorizer does not recognized the kind value, the Authorizer should report an error. + type: string + name: + description: Name of the object being referenced. + type: string + namespace: + description: Namespace of the referenced object. If the object kind is non-namespace, such as "User" or "Group", and this value is not empty the Authorizer should report an error. + type: string + required: + - kind + - name + type: object + x-kubernetes-map-type: atomic + type: array + type: object + type: array + limitRanges: + description: Specifies the resource min/max usage restrictions to the Space. Optional. + properties: + items: + items: + description: LimitRangeSpec defines a min/max usage limit for resources that match on kind. + properties: + limits: + description: Limits is the list of LimitRangeItem objects that are enforced. + items: + description: LimitRangeItem defines a min/max usage limit for any resource that matches on kind. + properties: + default: + additionalProperties: + anyOf: + - type: integer + - type: string + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + description: Default resource requirement limit value by resource name if resource limit is omitted. + type: object + defaultRequest: + additionalProperties: + anyOf: + - type: integer + - type: string + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + description: DefaultRequest is the default resource requirement request value by resource name if resource request is omitted. + type: object + max: + additionalProperties: + anyOf: + - type: integer + - type: string + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + description: Max usage constraints on this kind by resource name. + type: object + maxLimitRequestRatio: + additionalProperties: + anyOf: + - type: integer + - type: string + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + description: MaxLimitRequestRatio if specified, the named resource must have a request and limit that are both non-zero where limit divided by request is less than or equal to the enumerated value; this represents the max burst for the named resource. + type: object + min: + additionalProperties: + anyOf: + - type: integer + - type: string + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + description: Min usage constraints on this kind by resource name. + type: object + type: + description: Type of resource that this limit applies to. + type: string + required: + - type + type: object + type: array + required: + - limits + type: object + type: array + type: object + networkPolicies: + description: Specifies the NetworkPolicies assigned to the Tenant. The assigned NetworkPolicies are inherited by the namespace created in the Space. Optional. + properties: + enableDefaultStrictMode: + type: boolean + items: + items: + description: NetworkPolicySpec provides the specification of a NetworkPolicy + properties: + egress: + description: List of egress rules to be applied to the selected pods. Outgoing traffic is allowed if there are no NetworkPolicies selecting the pod (and cluster policy otherwise allows the traffic), OR if the traffic matches at least one egress rule across all of the NetworkPolicy objects whose podSelector matches the pod. If this field is empty then this NetworkPolicy limits all outgoing traffic (and serves solely to ensure that the pods it selects are isolated by default). This field is beta-level in 1.8 + items: + description: NetworkPolicyEgressRule describes a particular set of traffic that is allowed out of pods matched by a NetworkPolicySpec's podSelector. The traffic must match both ports and to. This type is beta-level in 1.8 + properties: + ports: + description: List of destination ports for outgoing traffic. Each item in this list is combined using a logical OR. If this field is empty or missing, this rule matches all ports (traffic not restricted by port). If this field is present and contains at least one item, then this rule allows traffic only if the traffic matches at least one port in the list. + items: + description: NetworkPolicyPort describes a port to allow traffic on + properties: + endPort: + description: If set, indicates that the range of ports from port to endPort, inclusive, should be allowed by the policy. This field cannot be defined if the port field is not defined or if the port field is defined as a named (string) port. The endPort must be equal or greater than port. + format: int32 + type: integer + port: + anyOf: + - type: integer + - type: string + description: The port on the given protocol. This can either be a numerical or named port on a pod. If this field is not provided, this matches all port names and numbers. If present, only traffic on the specified protocol AND port will be matched. + x-kubernetes-int-or-string: true + protocol: + default: TCP + description: The protocol (TCP, UDP, or SCTP) which traffic must match. If not specified, this field defaults to TCP. + type: string + type: object + type: array + to: + description: List of destinations for outgoing traffic of pods selected for this rule. Items in this list are combined using a logical OR operation. If this field is empty or missing, this rule matches all destinations (traffic not restricted by destination). If this field is present and contains at least one item, this rule allows traffic only if the traffic matches at least one item in the to list. + items: + description: NetworkPolicyPeer describes a peer to allow traffic to/from. Only certain combinations of fields are allowed + properties: + ipBlock: + description: IPBlock defines policy on a particular IPBlock. If this field is set then neither of the other fields can be. + properties: + cidr: + description: CIDR is a string representing the IP Block Valid examples are "192.168.1.1/24" or "2001:db9::/64" + type: string + except: + description: Except is a slice of CIDRs that should not be included within an IP Block Valid examples are "192.168.1.1/24" or "2001:db9::/64" Except values will be rejected if they are outside the CIDR range + items: + type: string + type: array + required: + - cidr + type: object + namespaceSelector: + description: "Selects Namespaces using cluster-scoped labels. This field follows standard label selector semantics; if present but empty, it selects all namespaces. \n If PodSelector is also set, then the NetworkPolicyPeer as a whole selects the Pods matching PodSelector in the Namespaces selected by NamespaceSelector. Otherwise it selects all Pods in the Namespaces selected by NamespaceSelector." + properties: + matchExpressions: + description: matchExpressions is a list of label selector requirements. The requirements are ANDed. + items: + description: A label selector requirement is a selector that contains values, a key, and an operator that relates the key and values. + properties: + key: + description: key is the label key that the selector applies to. + type: string + operator: + description: operator represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. + type: string + values: + description: values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch. + items: + type: string + type: array + required: + - key + - operator + type: object + type: array + matchLabels: + additionalProperties: + type: string + description: matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is "key", the operator is "In", and the values array contains only "value". The requirements are ANDed. + type: object + type: object + x-kubernetes-map-type: atomic + podSelector: + description: "This is a label selector which selects Pods. This field follows standard label selector semantics; if present but empty, it selects all pods. \n If NamespaceSelector is also set, then the NetworkPolicyPeer as a whole selects the Pods matching PodSelector in the Namespaces selected by NamespaceSelector. Otherwise it selects the Pods matching PodSelector in the policy's own Namespace." + properties: + matchExpressions: + description: matchExpressions is a list of label selector requirements. The requirements are ANDed. + items: + description: A label selector requirement is a selector that contains values, a key, and an operator that relates the key and values. + properties: + key: + description: key is the label key that the selector applies to. + type: string + operator: + description: operator represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. + type: string + values: + description: values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch. + items: + type: string + type: array + required: + - key + - operator + type: object + type: array + matchLabels: + additionalProperties: + type: string + description: matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is "key", the operator is "In", and the values array contains only "value". The requirements are ANDed. + type: object + type: object + x-kubernetes-map-type: atomic + type: object + type: array + type: object + type: array + ingress: + description: List of ingress rules to be applied to the selected pods. Traffic is allowed to a pod if there are no NetworkPolicies selecting the pod (and cluster policy otherwise allows the traffic), OR if the traffic source is the pod's local node, OR if the traffic matches at least one ingress rule across all of the NetworkPolicy objects whose podSelector matches the pod. If this field is empty then this NetworkPolicy does not allow any traffic (and serves solely to ensure that the pods it selects are isolated by default) + items: + description: NetworkPolicyIngressRule describes a particular set of traffic that is allowed to the pods matched by a NetworkPolicySpec's podSelector. The traffic must match both ports and from. + properties: + from: + description: List of sources which should be able to access the pods selected for this rule. Items in this list are combined using a logical OR operation. If this field is empty or missing, this rule matches all sources (traffic not restricted by source). If this field is present and contains at least one item, this rule allows traffic only if the traffic matches at least one item in the from list. + items: + description: NetworkPolicyPeer describes a peer to allow traffic to/from. Only certain combinations of fields are allowed + properties: + ipBlock: + description: IPBlock defines policy on a particular IPBlock. If this field is set then neither of the other fields can be. + properties: + cidr: + description: CIDR is a string representing the IP Block Valid examples are "192.168.1.1/24" or "2001:db9::/64" + type: string + except: + description: Except is a slice of CIDRs that should not be included within an IP Block Valid examples are "192.168.1.1/24" or "2001:db9::/64" Except values will be rejected if they are outside the CIDR range + items: + type: string + type: array + required: + - cidr + type: object + namespaceSelector: + description: "Selects Namespaces using cluster-scoped labels. This field follows standard label selector semantics; if present but empty, it selects all namespaces. \n If PodSelector is also set, then the NetworkPolicyPeer as a whole selects the Pods matching PodSelector in the Namespaces selected by NamespaceSelector. Otherwise it selects all Pods in the Namespaces selected by NamespaceSelector." + properties: + matchExpressions: + description: matchExpressions is a list of label selector requirements. The requirements are ANDed. + items: + description: A label selector requirement is a selector that contains values, a key, and an operator that relates the key and values. + properties: + key: + description: key is the label key that the selector applies to. + type: string + operator: + description: operator represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. + type: string + values: + description: values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch. + items: + type: string + type: array + required: + - key + - operator + type: object + type: array + matchLabels: + additionalProperties: + type: string + description: matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is "key", the operator is "In", and the values array contains only "value". The requirements are ANDed. + type: object + type: object + x-kubernetes-map-type: atomic + podSelector: + description: "This is a label selector which selects Pods. This field follows standard label selector semantics; if present but empty, it selects all pods. \n If NamespaceSelector is also set, then the NetworkPolicyPeer as a whole selects the Pods matching PodSelector in the Namespaces selected by NamespaceSelector. Otherwise it selects the Pods matching PodSelector in the policy's own Namespace." + properties: + matchExpressions: + description: matchExpressions is a list of label selector requirements. The requirements are ANDed. + items: + description: A label selector requirement is a selector that contains values, a key, and an operator that relates the key and values. + properties: + key: + description: key is the label key that the selector applies to. + type: string + operator: + description: operator represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. + type: string + values: + description: values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch. + items: + type: string + type: array + required: + - key + - operator + type: object + type: array + matchLabels: + additionalProperties: + type: string + description: matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is "key", the operator is "In", and the values array contains only "value". The requirements are ANDed. + type: object + type: object + x-kubernetes-map-type: atomic + type: object + type: array + ports: + description: List of ports which should be made accessible on the pods selected for this rule. Each item in this list is combined using a logical OR. If this field is empty or missing, this rule matches all ports (traffic not restricted by port). If this field is present and contains at least one item, then this rule allows traffic only if the traffic matches at least one port in the list. + items: + description: NetworkPolicyPort describes a port to allow traffic on + properties: + endPort: + description: If set, indicates that the range of ports from port to endPort, inclusive, should be allowed by the policy. This field cannot be defined if the port field is not defined or if the port field is defined as a named (string) port. The endPort must be equal or greater than port. + format: int32 + type: integer + port: + anyOf: + - type: integer + - type: string + description: The port on the given protocol. This can either be a numerical or named port on a pod. If this field is not provided, this matches all port names and numbers. If present, only traffic on the specified protocol AND port will be matched. + x-kubernetes-int-or-string: true + protocol: + default: TCP + description: The protocol (TCP, UDP, or SCTP) which traffic must match. If not specified, this field defaults to TCP. + type: string + type: object + type: array + type: object + type: array + podSelector: + description: Selects the pods to which this NetworkPolicy object applies. The array of ingress rules is applied to any pods selected by this field. Multiple network policies can select the same set of pods. In this case, the ingress rules for each are combined additively. This field is NOT optional and follows standard label selector semantics. An empty podSelector matches all pods in this namespace. + properties: + matchExpressions: + description: matchExpressions is a list of label selector requirements. The requirements are ANDed. + items: + description: A label selector requirement is a selector that contains values, a key, and an operator that relates the key and values. + properties: + key: + description: key is the label key that the selector applies to. + type: string + operator: + description: operator represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. + type: string + values: + description: values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch. + items: + type: string + type: array + required: + - key + - operator + type: object + type: array + matchLabels: + additionalProperties: + type: string + description: matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is "key", the operator is "In", and the values array contains only "value". The requirements are ANDed. + type: object + type: object + x-kubernetes-map-type: atomic + policyTypes: + description: List of rule types that the NetworkPolicy relates to. Valid options are ["Ingress"], ["Egress"], or ["Ingress", "Egress"]. If this field is not specified, it will default based on the existence of Ingress or Egress rules; policies that contain an Egress section are assumed to affect Egress, and all policies (whether or not they contain an Ingress section) are assumed to affect Ingress. If you want to write an egress-only policy, you must explicitly specify policyTypes [ "Egress" ]. Likewise, if you want to write a policy that specifies that no egress is allowed, you must specify a policyTypes value that include "Egress" (since such a policy would not include an Egress section and would otherwise default to just [ "Ingress" ]). This field is beta-level in 1.8 + items: + description: PolicyType string describes the NetworkPolicy type This type is beta-level in 1.8 + type: string + type: array + required: + - podSelector + type: object + type: array + type: object + resourceQuota: + description: Specifies a list of ResourceQuota resources assigned to the Space. The assigned values are inherited by the namespace created by the Space. Optional. + properties: + hard: + additionalProperties: + anyOf: + - type: integer + - type: string + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + description: 'hard is the set of desired hard limits for each named resource. More info: https://kubernetes.io/docs/concepts/policy/resource-quotas/' + type: object + scopeSelector: + description: scopeSelector is also a collection of filters like scopes that must match each object tracked by a quota but expressed using ScopeSelectorOperator in combination with possible values. For a resource to match, both scopes AND scopeSelector (if specified in spec), must be matched. + properties: + matchExpressions: + description: A list of scope selector requirements by scope of the resources. + items: + description: A scoped-resource selector requirement is a selector that contains values, a scope name, and an operator that relates the scope name and values. + properties: + operator: + description: Represents a scope's relationship to a set of values. Valid operators are In, NotIn, Exists, DoesNotExist. + type: string + scopeName: + description: The name of the scope that the selector applies to. + type: string + values: + description: An array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch. + items: + type: string + type: array + required: + - operator + - scopeName + type: object + type: array + type: object + x-kubernetes-map-type: atomic + scopes: + description: A collection of filters that must match each object tracked by a quota. If not specified, the quota matches all objects. + items: + description: A ResourceQuotaScope defines a filter that must match each object tracked by a quota + type: string + type: array + type: object + type: object + status: + description: SpaceTemplateStatus defines the observed state of SpaceTemplate. + properties: + conditions: + description: Conditions List of status conditions to indicate the status of Space + items: + description: "Condition contains details for one aspect of the current state of this API Resource. --- This struct is intended for direct use as an array at the field path .status.conditions. For example, \n type FooStatus struct{ // Represents the observations of a foo's current state. // Known .status.conditions.type are: \"Available\", \"Progressing\", and \"Degraded\" // +patchMergeKey=type // +patchStrategy=merge // +listType=map // +listMapKey=type Conditions []metav1.Condition `json:\"conditions,omitempty\" patchStrategy:\"merge\" patchMergeKey:\"type\" protobuf:\"bytes,1,rep,name=conditions\"` \n // other fields }" + properties: + lastTransitionTime: + description: lastTransitionTime is the last time the condition transitioned from one status to another. This should be when the underlying condition changed. If that is not known, then using the time when the API field changed is acceptable. + format: date-time + type: string + message: + description: message is a human readable message indicating details about the transition. This may be an empty string. + maxLength: 32768 + type: string + observedGeneration: + description: observedGeneration represents the .metadata.generation that the condition was set based upon. For instance, if .metadata.generation is currently 12, but the .status.conditions[x].observedGeneration is 9, the condition is out of date with respect to the current state of the instance. + format: int64 + minimum: 0 + type: integer + reason: + description: reason contains a programmatic identifier indicating the reason for the condition's last transition. Producers of specific condition types may define expected values and meanings for this field, and whether the values are considered a guaranteed API. The value should be a CamelCase string. This field may not be empty. + maxLength: 1024 + minLength: 1 + pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$ + type: string + status: + description: status of the condition, one of True, False, Unknown. + enum: + - "True" + - "False" + - Unknown + type: string + type: + description: type of condition in CamelCase or in foo.example.com/CamelCase. --- Many .condition.type values are consistent across resources like Available, but because arbitrary conditions can be useful (see .node.status.conditions), the ability to deconflict is important. The regex it matches is (dns1123SubdomainFmt/)?(qualifiedNameFmt) + maxLength: 316 + pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$ + type: string + required: + - lastTransitionTime + - message + - reason + - status + - type + type: object + type: array + type: object + type: object + served: true + storage: true + subresources: + status: {} diff --git a/config/crd/bases/nauticus.io_spaces.yaml b/config/crd/bases/nauticus.io_spaces.yaml index e091d74..1486b70 100644 --- a/config/crd/bases/nauticus.io_spaces.yaml +++ b/config/crd/bases/nauticus.io_spaces.yaml @@ -843,6 +843,20 @@ spec: type: object type: array type: object + templateRef: + description: Reference to a SpaceTemplate + properties: + group: + description: Group is the API group of the SpaceTemplate, "nauticus.io/v1alpha1". + type: string + kind: + description: Kind specifies the kind of the referenced resource, + which should be "SpaceTemplate". + type: string + name: + description: Name of the SpaceTemplate. + type: string + type: object type: object status: description: SpaceStatus defines the observed state of Space. diff --git a/config/crd/bases/nauticus.io_spacetemplates.yaml b/config/crd/bases/nauticus.io_spacetemplates.yaml new file mode 100644 index 0000000..8a8babc --- /dev/null +++ b/config/crd/bases/nauticus.io_spacetemplates.yaml @@ -0,0 +1,867 @@ +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.10.0 + creationTimestamp: null + name: spacetemplates.nauticus.io +spec: + group: nauticus.io + names: + categories: + - spacetemplate + kind: SpaceTemplate + listKind: SpaceTemplateList + plural: spacetemplates + singular: spacetemplate + scope: Cluster + versions: + - additionalPrinterColumns: + - description: Age + jsonPath: .metadata.creationTimestamp + name: Age + type: date + - description: Ready + jsonPath: .status.conditions[?(@.type=='Ready')].status + name: Ready + type: string + name: v1alpha1 + schema: + openAPIV3Schema: + description: SpaceTemplate is the Schema for the spacetemplates API. + properties: + apiVersion: + description: 'APIVersion defines the versioned schema of this representation + of an object. Servers should convert recognized schemas to the latest + internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources' + type: string + kind: + description: 'Kind is a string value representing the REST resource this + object represents. Servers may infer this from the endpoint the client + submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' + type: string + metadata: + type: object + spec: + description: SpaceTemplateSpec defines the desired state of SpaceTemplate. + properties: + additionalRoleBindings: + description: Specifies additional RoleBindings assigned to the Space. + Nauticus will ensure that the namespace in the Space always contain + the RoleBinding for the given ClusterRole. Optional. + items: + properties: + roleRef: + description: RoleRef contains information that points to the + role being used + properties: + apiGroup: + description: APIGroup is the group for the resource being + referenced + type: string + kind: + description: Kind is the type of resource being referenced + type: string + name: + description: Name is the name of resource being referenced + type: string + required: + - apiGroup + - kind + - name + type: object + x-kubernetes-map-type: atomic + subjects: + items: + description: Subject contains a reference to the object or + user identities a role binding applies to. This can either + hold a direct API object reference, or a value for non-objects + such as user and group names. + properties: + apiGroup: + description: APIGroup holds the API group of the referenced + subject. Defaults to "" for ServiceAccount subjects. + Defaults to "rbac.authorization.k8s.io" for User and + Group subjects. + type: string + kind: + description: Kind of object being referenced. Values defined + by this API group are "User", "Group", and "ServiceAccount". + If the Authorizer does not recognized the kind value, + the Authorizer should report an error. + type: string + name: + description: Name of the object being referenced. + type: string + namespace: + description: Namespace of the referenced object. If the + object kind is non-namespace, such as "User" or "Group", + and this value is not empty the Authorizer should report + an error. + type: string + required: + - kind + - name + type: object + x-kubernetes-map-type: atomic + type: array + type: object + type: array + limitRanges: + description: Specifies the resource min/max usage restrictions to + the Space. Optional. + properties: + items: + items: + description: LimitRangeSpec defines a min/max usage limit for + resources that match on kind. + properties: + limits: + description: Limits is the list of LimitRangeItem objects + that are enforced. + items: + description: LimitRangeItem defines a min/max usage limit + for any resource that matches on kind. + properties: + default: + additionalProperties: + anyOf: + - type: integer + - type: string + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + description: Default resource requirement limit value + by resource name if resource limit is omitted. + type: object + defaultRequest: + additionalProperties: + anyOf: + - type: integer + - type: string + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + description: DefaultRequest is the default resource + requirement request value by resource name if resource + request is omitted. + type: object + max: + additionalProperties: + anyOf: + - type: integer + - type: string + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + description: Max usage constraints on this kind by + resource name. + type: object + maxLimitRequestRatio: + additionalProperties: + anyOf: + - type: integer + - type: string + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + description: MaxLimitRequestRatio if specified, the + named resource must have a request and limit that + are both non-zero where limit divided by request + is less than or equal to the enumerated value; this + represents the max burst for the named resource. + type: object + min: + additionalProperties: + anyOf: + - type: integer + - type: string + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + description: Min usage constraints on this kind by + resource name. + type: object + type: + description: Type of resource that this limit applies + to. + type: string + required: + - type + type: object + type: array + required: + - limits + type: object + type: array + type: object + networkPolicies: + description: Specifies the NetworkPolicies assigned to the Tenant. + The assigned NetworkPolicies are inherited by the namespace created + in the Space. Optional. + properties: + enableDefaultStrictMode: + type: boolean + items: + items: + description: NetworkPolicySpec provides the specification of + a NetworkPolicy + properties: + egress: + description: List of egress rules to be applied to the selected + pods. Outgoing traffic is allowed if there are no NetworkPolicies + selecting the pod (and cluster policy otherwise allows + the traffic), OR if the traffic matches at least one egress + rule across all of the NetworkPolicy objects whose podSelector + matches the pod. If this field is empty then this NetworkPolicy + limits all outgoing traffic (and serves solely to ensure + that the pods it selects are isolated by default). This + field is beta-level in 1.8 + items: + description: NetworkPolicyEgressRule describes a particular + set of traffic that is allowed out of pods matched by + a NetworkPolicySpec's podSelector. The traffic must + match both ports and to. This type is beta-level in + 1.8 + properties: + ports: + description: List of destination ports for outgoing + traffic. Each item in this list is combined using + a logical OR. If this field is empty or missing, + this rule matches all ports (traffic not restricted + by port). If this field is present and contains + at least one item, then this rule allows traffic + only if the traffic matches at least one port in + the list. + items: + description: NetworkPolicyPort describes a port + to allow traffic on + properties: + endPort: + description: If set, indicates that the range + of ports from port to endPort, inclusive, + should be allowed by the policy. This field + cannot be defined if the port field is not + defined or if the port field is defined as + a named (string) port. The endPort must be + equal or greater than port. + format: int32 + type: integer + port: + anyOf: + - type: integer + - type: string + description: The port on the given protocol. + This can either be a numerical or named port + on a pod. If this field is not provided, this + matches all port names and numbers. If present, + only traffic on the specified protocol AND + port will be matched. + x-kubernetes-int-or-string: true + protocol: + default: TCP + description: The protocol (TCP, UDP, or SCTP) + which traffic must match. If not specified, + this field defaults to TCP. + type: string + type: object + type: array + to: + description: List of destinations for outgoing traffic + of pods selected for this rule. Items in this list + are combined using a logical OR operation. If this + field is empty or missing, this rule matches all + destinations (traffic not restricted by destination). + If this field is present and contains at least one + item, this rule allows traffic only if the traffic + matches at least one item in the to list. + items: + description: NetworkPolicyPeer describes a peer + to allow traffic to/from. Only certain combinations + of fields are allowed + properties: + ipBlock: + description: IPBlock defines policy on a particular + IPBlock. If this field is set then neither + of the other fields can be. + properties: + cidr: + description: CIDR is a string representing + the IP Block Valid examples are "192.168.1.1/24" + or "2001:db9::/64" + type: string + except: + description: Except is a slice of CIDRs + that should not be included within an + IP Block Valid examples are "192.168.1.1/24" + or "2001:db9::/64" Except values will + be rejected if they are outside the CIDR + range + items: + type: string + type: array + required: + - cidr + type: object + namespaceSelector: + description: "Selects Namespaces using cluster-scoped + labels. This field follows standard label + selector semantics; if present but empty, + it selects all namespaces. \n If PodSelector + is also set, then the NetworkPolicyPeer as + a whole selects the Pods matching PodSelector + in the Namespaces selected by NamespaceSelector. + Otherwise it selects all Pods in the Namespaces + selected by NamespaceSelector." + properties: + matchExpressions: + description: matchExpressions is a list + of label selector requirements. The requirements + are ANDed. + items: + description: A label selector requirement + is a selector that contains values, + a key, and an operator that relates + the key and values. + properties: + key: + description: key is the label key + that the selector applies to. + type: string + operator: + description: operator represents a + key's relationship to a set of values. + Valid operators are In, NotIn, Exists + and DoesNotExist. + type: string + values: + description: values is an array of + string values. If the operator is + In or NotIn, the values array must + be non-empty. If the operator is + Exists or DoesNotExist, the values + array must be empty. This array + is replaced during a strategic merge + patch. + items: + type: string + type: array + required: + - key + - operator + type: object + type: array + matchLabels: + additionalProperties: + type: string + description: matchLabels is a map of {key,value} + pairs. A single {key,value} in the matchLabels + map is equivalent to an element of matchExpressions, + whose key field is "key", the operator + is "In", and the values array contains + only "value". The requirements are ANDed. + type: object + type: object + x-kubernetes-map-type: atomic + podSelector: + description: "This is a label selector which + selects Pods. This field follows standard + label selector semantics; if present but empty, + it selects all pods. \n If NamespaceSelector + is also set, then the NetworkPolicyPeer as + a whole selects the Pods matching PodSelector + in the Namespaces selected by NamespaceSelector. + Otherwise it selects the Pods matching PodSelector + in the policy's own Namespace." + properties: + matchExpressions: + description: matchExpressions is a list + of label selector requirements. The requirements + are ANDed. + items: + description: A label selector requirement + is a selector that contains values, + a key, and an operator that relates + the key and values. + properties: + key: + description: key is the label key + that the selector applies to. + type: string + operator: + description: operator represents a + key's relationship to a set of values. + Valid operators are In, NotIn, Exists + and DoesNotExist. + type: string + values: + description: values is an array of + string values. If the operator is + In or NotIn, the values array must + be non-empty. If the operator is + Exists or DoesNotExist, the values + array must be empty. This array + is replaced during a strategic merge + patch. + items: + type: string + type: array + required: + - key + - operator + type: object + type: array + matchLabels: + additionalProperties: + type: string + description: matchLabels is a map of {key,value} + pairs. A single {key,value} in the matchLabels + map is equivalent to an element of matchExpressions, + whose key field is "key", the operator + is "In", and the values array contains + only "value". The requirements are ANDed. + type: object + type: object + x-kubernetes-map-type: atomic + type: object + type: array + type: object + type: array + ingress: + description: List of ingress rules to be applied to the + selected pods. Traffic is allowed to a pod if there are + no NetworkPolicies selecting the pod (and cluster policy + otherwise allows the traffic), OR if the traffic source + is the pod's local node, OR if the traffic matches at + least one ingress rule across all of the NetworkPolicy + objects whose podSelector matches the pod. If this field + is empty then this NetworkPolicy does not allow any traffic + (and serves solely to ensure that the pods it selects + are isolated by default) + items: + description: NetworkPolicyIngressRule describes a particular + set of traffic that is allowed to the pods matched by + a NetworkPolicySpec's podSelector. The traffic must + match both ports and from. + properties: + from: + description: List of sources which should be able + to access the pods selected for this rule. Items + in this list are combined using a logical OR operation. + If this field is empty or missing, this rule matches + all sources (traffic not restricted by source). + If this field is present and contains at least one + item, this rule allows traffic only if the traffic + matches at least one item in the from list. + items: + description: NetworkPolicyPeer describes a peer + to allow traffic to/from. Only certain combinations + of fields are allowed + properties: + ipBlock: + description: IPBlock defines policy on a particular + IPBlock. If this field is set then neither + of the other fields can be. + properties: + cidr: + description: CIDR is a string representing + the IP Block Valid examples are "192.168.1.1/24" + or "2001:db9::/64" + type: string + except: + description: Except is a slice of CIDRs + that should not be included within an + IP Block Valid examples are "192.168.1.1/24" + or "2001:db9::/64" Except values will + be rejected if they are outside the CIDR + range + items: + type: string + type: array + required: + - cidr + type: object + namespaceSelector: + description: "Selects Namespaces using cluster-scoped + labels. This field follows standard label + selector semantics; if present but empty, + it selects all namespaces. \n If PodSelector + is also set, then the NetworkPolicyPeer as + a whole selects the Pods matching PodSelector + in the Namespaces selected by NamespaceSelector. + Otherwise it selects all Pods in the Namespaces + selected by NamespaceSelector." + properties: + matchExpressions: + description: matchExpressions is a list + of label selector requirements. The requirements + are ANDed. + items: + description: A label selector requirement + is a selector that contains values, + a key, and an operator that relates + the key and values. + properties: + key: + description: key is the label key + that the selector applies to. + type: string + operator: + description: operator represents a + key's relationship to a set of values. + Valid operators are In, NotIn, Exists + and DoesNotExist. + type: string + values: + description: values is an array of + string values. If the operator is + In or NotIn, the values array must + be non-empty. If the operator is + Exists or DoesNotExist, the values + array must be empty. This array + is replaced during a strategic merge + patch. + items: + type: string + type: array + required: + - key + - operator + type: object + type: array + matchLabels: + additionalProperties: + type: string + description: matchLabels is a map of {key,value} + pairs. A single {key,value} in the matchLabels + map is equivalent to an element of matchExpressions, + whose key field is "key", the operator + is "In", and the values array contains + only "value". The requirements are ANDed. + type: object + type: object + x-kubernetes-map-type: atomic + podSelector: + description: "This is a label selector which + selects Pods. This field follows standard + label selector semantics; if present but empty, + it selects all pods. \n If NamespaceSelector + is also set, then the NetworkPolicyPeer as + a whole selects the Pods matching PodSelector + in the Namespaces selected by NamespaceSelector. + Otherwise it selects the Pods matching PodSelector + in the policy's own Namespace." + properties: + matchExpressions: + description: matchExpressions is a list + of label selector requirements. The requirements + are ANDed. + items: + description: A label selector requirement + is a selector that contains values, + a key, and an operator that relates + the key and values. + properties: + key: + description: key is the label key + that the selector applies to. + type: string + operator: + description: operator represents a + key's relationship to a set of values. + Valid operators are In, NotIn, Exists + and DoesNotExist. + type: string + values: + description: values is an array of + string values. If the operator is + In or NotIn, the values array must + be non-empty. If the operator is + Exists or DoesNotExist, the values + array must be empty. This array + is replaced during a strategic merge + patch. + items: + type: string + type: array + required: + - key + - operator + type: object + type: array + matchLabels: + additionalProperties: + type: string + description: matchLabels is a map of {key,value} + pairs. A single {key,value} in the matchLabels + map is equivalent to an element of matchExpressions, + whose key field is "key", the operator + is "In", and the values array contains + only "value". The requirements are ANDed. + type: object + type: object + x-kubernetes-map-type: atomic + type: object + type: array + ports: + description: List of ports which should be made accessible + on the pods selected for this rule. Each item in + this list is combined using a logical OR. If this + field is empty or missing, this rule matches all + ports (traffic not restricted by port). If this + field is present and contains at least one item, + then this rule allows traffic only if the traffic + matches at least one port in the list. + items: + description: NetworkPolicyPort describes a port + to allow traffic on + properties: + endPort: + description: If set, indicates that the range + of ports from port to endPort, inclusive, + should be allowed by the policy. This field + cannot be defined if the port field is not + defined or if the port field is defined as + a named (string) port. The endPort must be + equal or greater than port. + format: int32 + type: integer + port: + anyOf: + - type: integer + - type: string + description: The port on the given protocol. + This can either be a numerical or named port + on a pod. If this field is not provided, this + matches all port names and numbers. If present, + only traffic on the specified protocol AND + port will be matched. + x-kubernetes-int-or-string: true + protocol: + default: TCP + description: The protocol (TCP, UDP, or SCTP) + which traffic must match. If not specified, + this field defaults to TCP. + type: string + type: object + type: array + type: object + type: array + podSelector: + description: Selects the pods to which this NetworkPolicy + object applies. The array of ingress rules is applied + to any pods selected by this field. Multiple network policies + can select the same set of pods. In this case, the ingress + rules for each are combined additively. This field is + NOT optional and follows standard label selector semantics. + An empty podSelector matches all pods in this namespace. + properties: + matchExpressions: + description: matchExpressions is a list of label selector + requirements. The requirements are ANDed. + items: + description: A label selector requirement is a selector + that contains values, a key, and an operator that + relates the key and values. + properties: + key: + description: key is the label key that the selector + applies to. + type: string + operator: + description: operator represents a key's relationship + to a set of values. Valid operators are In, + NotIn, Exists and DoesNotExist. + type: string + values: + description: values is an array of string values. + If the operator is In or NotIn, the values array + must be non-empty. If the operator is Exists + or DoesNotExist, the values array must be empty. + This array is replaced during a strategic merge + patch. + items: + type: string + type: array + required: + - key + - operator + type: object + type: array + matchLabels: + additionalProperties: + type: string + description: matchLabels is a map of {key,value} pairs. + A single {key,value} in the matchLabels map is equivalent + to an element of matchExpressions, whose key field + is "key", the operator is "In", and the values array + contains only "value". The requirements are ANDed. + type: object + type: object + x-kubernetes-map-type: atomic + policyTypes: + description: List of rule types that the NetworkPolicy relates + to. Valid options are ["Ingress"], ["Egress"], or ["Ingress", + "Egress"]. If this field is not specified, it will default + based on the existence of Ingress or Egress rules; policies + that contain an Egress section are assumed to affect Egress, + and all policies (whether or not they contain an Ingress + section) are assumed to affect Ingress. If you want to + write an egress-only policy, you must explicitly specify + policyTypes [ "Egress" ]. Likewise, if you want to write + a policy that specifies that no egress is allowed, you + must specify a policyTypes value that include "Egress" + (since such a policy would not include an Egress section + and would otherwise default to just [ "Ingress" ]). This + field is beta-level in 1.8 + items: + description: PolicyType string describes the NetworkPolicy + type This type is beta-level in 1.8 + type: string + type: array + required: + - podSelector + type: object + type: array + type: object + resourceQuota: + description: Specifies a list of ResourceQuota resources assigned + to the Space. The assigned values are inherited by the namespace + created by the Space. Optional. + properties: + hard: + additionalProperties: + anyOf: + - type: integer + - type: string + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + description: 'hard is the set of desired hard limits for each + named resource. More info: https://kubernetes.io/docs/concepts/policy/resource-quotas/' + type: object + scopeSelector: + description: scopeSelector is also a collection of filters like + scopes that must match each object tracked by a quota but expressed + using ScopeSelectorOperator in combination with possible values. + For a resource to match, both scopes AND scopeSelector (if specified + in spec), must be matched. + properties: + matchExpressions: + description: A list of scope selector requirements by scope + of the resources. + items: + description: A scoped-resource selector requirement is a + selector that contains values, a scope name, and an operator + that relates the scope name and values. + properties: + operator: + description: Represents a scope's relationship to a + set of values. Valid operators are In, NotIn, Exists, + DoesNotExist. + type: string + scopeName: + description: The name of the scope that the selector + applies to. + type: string + values: + description: An array of string values. If the operator + is In or NotIn, the values array must be non-empty. + If the operator is Exists or DoesNotExist, the values + array must be empty. This array is replaced during + a strategic merge patch. + items: + type: string + type: array + required: + - operator + - scopeName + type: object + type: array + type: object + x-kubernetes-map-type: atomic + scopes: + description: A collection of filters that must match each object + tracked by a quota. If not specified, the quota matches all + objects. + items: + description: A ResourceQuotaScope defines a filter that must + match each object tracked by a quota + type: string + type: array + type: object + type: object + status: + description: SpaceTemplateStatus defines the observed state of SpaceTemplate. + properties: + conditions: + description: Conditions List of status conditions to indicate the + status of Space + items: + description: "Condition contains details for one aspect of the current + state of this API Resource. --- This struct is intended for direct + use as an array at the field path .status.conditions. For example, + \n type FooStatus struct{ // Represents the observations of a + foo's current state. // Known .status.conditions.type are: \"Available\", + \"Progressing\", and \"Degraded\" // +patchMergeKey=type // +patchStrategy=merge + // +listType=map // +listMapKey=type Conditions []metav1.Condition + `json:\"conditions,omitempty\" patchStrategy:\"merge\" patchMergeKey:\"type\" + protobuf:\"bytes,1,rep,name=conditions\"` \n // other fields }" + properties: + lastTransitionTime: + description: lastTransitionTime is the last time the condition + transitioned from one status to another. This should be when + the underlying condition changed. If that is not known, then + using the time when the API field changed is acceptable. + format: date-time + type: string + message: + description: message is a human readable message indicating + details about the transition. This may be an empty string. + maxLength: 32768 + type: string + observedGeneration: + description: observedGeneration represents the .metadata.generation + that the condition was set based upon. For instance, if .metadata.generation + is currently 12, but the .status.conditions[x].observedGeneration + is 9, the condition is out of date with respect to the current + state of the instance. + format: int64 + minimum: 0 + type: integer + reason: + description: reason contains a programmatic identifier indicating + the reason for the condition's last transition. Producers + of specific condition types may define expected values and + meanings for this field, and whether the values are considered + a guaranteed API. The value should be a CamelCase string. + This field may not be empty. + maxLength: 1024 + minLength: 1 + pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$ + type: string + status: + description: status of the condition, one of True, False, Unknown. + enum: + - "True" + - "False" + - Unknown + type: string + type: + description: type of condition in CamelCase or in foo.example.com/CamelCase. + --- Many .condition.type values are consistent across resources + like Available, but because arbitrary conditions can be useful + (see .node.status.conditions), the ability to deconflict is + important. The regex it matches is (dns1123SubdomainFmt/)?(qualifiedNameFmt) + maxLength: 316 + pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$ + type: string + required: + - lastTransitionTime + - message + - reason + - status + - type + type: object + type: array + type: object + type: object + served: true + storage: true + subresources: + status: {} diff --git a/config/crd/kustomization.yaml b/config/crd/kustomization.yaml index 4c894cc..db1e7c8 100644 --- a/config/crd/kustomization.yaml +++ b/config/crd/kustomization.yaml @@ -3,17 +3,20 @@ # It should be run by config/default resources: - bases/nauticus.io_spaces.yaml +- bases/nauticus.io_spacetemplates.yaml #+kubebuilder:scaffold:crdkustomizeresource # patchesStrategicMerge: # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix. # patches here are for enabling the conversion webhook for each CRD #- patches/webhook_in_spaces.yaml +#- patches/webhook_in_spacetemplates.yaml #+kubebuilder:scaffold:crdkustomizewebhookpatch # [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix. # patches here are for enabling the CA injection for each CRD #- patches/cainjection_in_spaces.yaml +#- patches/cainjection_in_spacetemplates.yaml #+kubebuilder:scaffold:crdkustomizecainjectionpatch # the following config is for teaching kustomize how to do kustomization for CRDs. diff --git a/config/crd/patches/cainjection_in_spacetemplates.yaml b/config/crd/patches/cainjection_in_spacetemplates.yaml new file mode 100644 index 0000000..a3fd0cc --- /dev/null +++ b/config/crd/patches/cainjection_in_spacetemplates.yaml @@ -0,0 +1,7 @@ +# The following patch adds a directive for certmanager to inject CA into the CRD +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME) + name: spacetemplates.nauticus.io diff --git a/config/crd/patches/webhook_in_spacetemplates.yaml b/config/crd/patches/webhook_in_spacetemplates.yaml new file mode 100644 index 0000000..d3352ac --- /dev/null +++ b/config/crd/patches/webhook_in_spacetemplates.yaml @@ -0,0 +1,16 @@ +# The following patch enables a conversion webhook for the CRD +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + name: spacetemplates.io.nauticus.io +spec: + conversion: + strategy: Webhook + webhook: + clientConfig: + service: + namespace: system + name: webhook-service + path: /convert + conversionReviewVersions: + - v1 diff --git a/config/install.yaml b/config/install.yaml index 671342a..4f5a94e 100644 --- a/config/install.yaml +++ b/config/install.yaml @@ -531,9 +531,556 @@ spec: type: object type: array type: object + templateRef: + description: Reference to a SpaceTemplate + properties: + group: + description: Group is the API group of the SpaceTemplate, "nauticus.io/v1alpha1". + type: string + kind: + description: Kind specifies the kind of the referenced resource, which should be "SpaceTemplate". + type: string + name: + description: Name of the SpaceTemplate. + type: string + type: object + type: object + status: + description: SpaceStatus defines the observed state of Space. + properties: + conditions: + description: Conditions List of status conditions to indicate the status of Space + items: + description: "Condition contains details for one aspect of the current state of this API Resource. --- This struct is intended for direct use as an array at the field path .status.conditions. For example, \n type FooStatus struct{ // Represents the observations of a foo's current state. // Known .status.conditions.type are: \"Available\", \"Progressing\", and \"Degraded\" // +patchMergeKey=type // +patchStrategy=merge // +listType=map // +listMapKey=type Conditions []metav1.Condition `json:\"conditions,omitempty\" patchStrategy:\"merge\" patchMergeKey:\"type\" protobuf:\"bytes,1,rep,name=conditions\"` \n // other fields }" + properties: + lastTransitionTime: + description: lastTransitionTime is the last time the condition transitioned from one status to another. This should be when the underlying condition changed. If that is not known, then using the time when the API field changed is acceptable. + format: date-time + type: string + message: + description: message is a human readable message indicating details about the transition. This may be an empty string. + maxLength: 32768 + type: string + observedGeneration: + description: observedGeneration represents the .metadata.generation that the condition was set based upon. For instance, if .metadata.generation is currently 12, but the .status.conditions[x].observedGeneration is 9, the condition is out of date with respect to the current state of the instance. + format: int64 + minimum: 0 + type: integer + reason: + description: reason contains a programmatic identifier indicating the reason for the condition's last transition. Producers of specific condition types may define expected values and meanings for this field, and whether the values are considered a guaranteed API. The value should be a CamelCase string. This field may not be empty. + maxLength: 1024 + minLength: 1 + pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$ + type: string + status: + description: status of the condition, one of True, False, Unknown. + enum: + - "True" + - "False" + - Unknown + type: string + type: + description: type of condition in CamelCase or in foo.example.com/CamelCase. --- Many .condition.type values are consistent across resources like Available, but because arbitrary conditions can be useful (see .node.status.conditions), the ability to deconflict is important. The regex it matches is (dns1123SubdomainFmt/)?(qualifiedNameFmt) + maxLength: 316 + pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$ + type: string + required: + - lastTransitionTime + - message + - reason + - status + - type + type: object + type: array + namespaceName: + description: NamespaceName the name of the created underlying namespace. + type: string + type: object + type: object + served: true + storage: true + subresources: + status: {} +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.10.0 + creationTimestamp: null + name: spacetemplates.nauticus.io +spec: + group: nauticus.io + names: + categories: + - spacetemplate + kind: SpaceTemplate + listKind: SpaceTemplateList + plural: spacetemplates + singular: spacetemplate + scope: Cluster + versions: + - additionalPrinterColumns: + - description: Age + jsonPath: .metadata.creationTimestamp + name: Age + type: date + - description: Ready + jsonPath: .status.conditions[?(@.type=='Ready')].status + name: Ready + type: string + name: v1alpha1 + schema: + openAPIV3Schema: + description: SpaceTemplate is the Schema for the spacetemplates API. + properties: + apiVersion: + description: 'APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources' + type: string + kind: + description: 'Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' + type: string + metadata: + type: object + spec: + description: SpaceTemplateSpec defines the desired state of SpaceTemplate. + properties: + additionalRoleBindings: + description: Specifies additional RoleBindings assigned to the Space. Nauticus will ensure that the namespace in the Space always contain the RoleBinding for the given ClusterRole. Optional. + items: + properties: + roleRef: + description: RoleRef contains information that points to the role being used + properties: + apiGroup: + description: APIGroup is the group for the resource being referenced + type: string + kind: + description: Kind is the type of resource being referenced + type: string + name: + description: Name is the name of resource being referenced + type: string + required: + - apiGroup + - kind + - name + type: object + x-kubernetes-map-type: atomic + subjects: + items: + description: Subject contains a reference to the object or user identities a role binding applies to. This can either hold a direct API object reference, or a value for non-objects such as user and group names. + properties: + apiGroup: + description: APIGroup holds the API group of the referenced subject. Defaults to "" for ServiceAccount subjects. Defaults to "rbac.authorization.k8s.io" for User and Group subjects. + type: string + kind: + description: Kind of object being referenced. Values defined by this API group are "User", "Group", and "ServiceAccount". If the Authorizer does not recognized the kind value, the Authorizer should report an error. + type: string + name: + description: Name of the object being referenced. + type: string + namespace: + description: Namespace of the referenced object. If the object kind is non-namespace, such as "User" or "Group", and this value is not empty the Authorizer should report an error. + type: string + required: + - kind + - name + type: object + x-kubernetes-map-type: atomic + type: array + type: object + type: array + limitRanges: + description: Specifies the resource min/max usage restrictions to the Space. Optional. + properties: + items: + items: + description: LimitRangeSpec defines a min/max usage limit for resources that match on kind. + properties: + limits: + description: Limits is the list of LimitRangeItem objects that are enforced. + items: + description: LimitRangeItem defines a min/max usage limit for any resource that matches on kind. + properties: + default: + additionalProperties: + anyOf: + - type: integer + - type: string + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + description: Default resource requirement limit value by resource name if resource limit is omitted. + type: object + defaultRequest: + additionalProperties: + anyOf: + - type: integer + - type: string + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + description: DefaultRequest is the default resource requirement request value by resource name if resource request is omitted. + type: object + max: + additionalProperties: + anyOf: + - type: integer + - type: string + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + description: Max usage constraints on this kind by resource name. + type: object + maxLimitRequestRatio: + additionalProperties: + anyOf: + - type: integer + - type: string + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + description: MaxLimitRequestRatio if specified, the named resource must have a request and limit that are both non-zero where limit divided by request is less than or equal to the enumerated value; this represents the max burst for the named resource. + type: object + min: + additionalProperties: + anyOf: + - type: integer + - type: string + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + description: Min usage constraints on this kind by resource name. + type: object + type: + description: Type of resource that this limit applies to. + type: string + required: + - type + type: object + type: array + required: + - limits + type: object + type: array + type: object + networkPolicies: + description: Specifies the NetworkPolicies assigned to the Tenant. The assigned NetworkPolicies are inherited by the namespace created in the Space. Optional. + properties: + enableDefaultStrictMode: + type: boolean + items: + items: + description: NetworkPolicySpec provides the specification of a NetworkPolicy + properties: + egress: + description: List of egress rules to be applied to the selected pods. Outgoing traffic is allowed if there are no NetworkPolicies selecting the pod (and cluster policy otherwise allows the traffic), OR if the traffic matches at least one egress rule across all of the NetworkPolicy objects whose podSelector matches the pod. If this field is empty then this NetworkPolicy limits all outgoing traffic (and serves solely to ensure that the pods it selects are isolated by default). This field is beta-level in 1.8 + items: + description: NetworkPolicyEgressRule describes a particular set of traffic that is allowed out of pods matched by a NetworkPolicySpec's podSelector. The traffic must match both ports and to. This type is beta-level in 1.8 + properties: + ports: + description: List of destination ports for outgoing traffic. Each item in this list is combined using a logical OR. If this field is empty or missing, this rule matches all ports (traffic not restricted by port). If this field is present and contains at least one item, then this rule allows traffic only if the traffic matches at least one port in the list. + items: + description: NetworkPolicyPort describes a port to allow traffic on + properties: + endPort: + description: If set, indicates that the range of ports from port to endPort, inclusive, should be allowed by the policy. This field cannot be defined if the port field is not defined or if the port field is defined as a named (string) port. The endPort must be equal or greater than port. + format: int32 + type: integer + port: + anyOf: + - type: integer + - type: string + description: The port on the given protocol. This can either be a numerical or named port on a pod. If this field is not provided, this matches all port names and numbers. If present, only traffic on the specified protocol AND port will be matched. + x-kubernetes-int-or-string: true + protocol: + default: TCP + description: The protocol (TCP, UDP, or SCTP) which traffic must match. If not specified, this field defaults to TCP. + type: string + type: object + type: array + to: + description: List of destinations for outgoing traffic of pods selected for this rule. Items in this list are combined using a logical OR operation. If this field is empty or missing, this rule matches all destinations (traffic not restricted by destination). If this field is present and contains at least one item, this rule allows traffic only if the traffic matches at least one item in the to list. + items: + description: NetworkPolicyPeer describes a peer to allow traffic to/from. Only certain combinations of fields are allowed + properties: + ipBlock: + description: IPBlock defines policy on a particular IPBlock. If this field is set then neither of the other fields can be. + properties: + cidr: + description: CIDR is a string representing the IP Block Valid examples are "192.168.1.1/24" or "2001:db9::/64" + type: string + except: + description: Except is a slice of CIDRs that should not be included within an IP Block Valid examples are "192.168.1.1/24" or "2001:db9::/64" Except values will be rejected if they are outside the CIDR range + items: + type: string + type: array + required: + - cidr + type: object + namespaceSelector: + description: "Selects Namespaces using cluster-scoped labels. This field follows standard label selector semantics; if present but empty, it selects all namespaces. \n If PodSelector is also set, then the NetworkPolicyPeer as a whole selects the Pods matching PodSelector in the Namespaces selected by NamespaceSelector. Otherwise it selects all Pods in the Namespaces selected by NamespaceSelector." + properties: + matchExpressions: + description: matchExpressions is a list of label selector requirements. The requirements are ANDed. + items: + description: A label selector requirement is a selector that contains values, a key, and an operator that relates the key and values. + properties: + key: + description: key is the label key that the selector applies to. + type: string + operator: + description: operator represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. + type: string + values: + description: values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch. + items: + type: string + type: array + required: + - key + - operator + type: object + type: array + matchLabels: + additionalProperties: + type: string + description: matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is "key", the operator is "In", and the values array contains only "value". The requirements are ANDed. + type: object + type: object + x-kubernetes-map-type: atomic + podSelector: + description: "This is a label selector which selects Pods. This field follows standard label selector semantics; if present but empty, it selects all pods. \n If NamespaceSelector is also set, then the NetworkPolicyPeer as a whole selects the Pods matching PodSelector in the Namespaces selected by NamespaceSelector. Otherwise it selects the Pods matching PodSelector in the policy's own Namespace." + properties: + matchExpressions: + description: matchExpressions is a list of label selector requirements. The requirements are ANDed. + items: + description: A label selector requirement is a selector that contains values, a key, and an operator that relates the key and values. + properties: + key: + description: key is the label key that the selector applies to. + type: string + operator: + description: operator represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. + type: string + values: + description: values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch. + items: + type: string + type: array + required: + - key + - operator + type: object + type: array + matchLabels: + additionalProperties: + type: string + description: matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is "key", the operator is "In", and the values array contains only "value". The requirements are ANDed. + type: object + type: object + x-kubernetes-map-type: atomic + type: object + type: array + type: object + type: array + ingress: + description: List of ingress rules to be applied to the selected pods. Traffic is allowed to a pod if there are no NetworkPolicies selecting the pod (and cluster policy otherwise allows the traffic), OR if the traffic source is the pod's local node, OR if the traffic matches at least one ingress rule across all of the NetworkPolicy objects whose podSelector matches the pod. If this field is empty then this NetworkPolicy does not allow any traffic (and serves solely to ensure that the pods it selects are isolated by default) + items: + description: NetworkPolicyIngressRule describes a particular set of traffic that is allowed to the pods matched by a NetworkPolicySpec's podSelector. The traffic must match both ports and from. + properties: + from: + description: List of sources which should be able to access the pods selected for this rule. Items in this list are combined using a logical OR operation. If this field is empty or missing, this rule matches all sources (traffic not restricted by source). If this field is present and contains at least one item, this rule allows traffic only if the traffic matches at least one item in the from list. + items: + description: NetworkPolicyPeer describes a peer to allow traffic to/from. Only certain combinations of fields are allowed + properties: + ipBlock: + description: IPBlock defines policy on a particular IPBlock. If this field is set then neither of the other fields can be. + properties: + cidr: + description: CIDR is a string representing the IP Block Valid examples are "192.168.1.1/24" or "2001:db9::/64" + type: string + except: + description: Except is a slice of CIDRs that should not be included within an IP Block Valid examples are "192.168.1.1/24" or "2001:db9::/64" Except values will be rejected if they are outside the CIDR range + items: + type: string + type: array + required: + - cidr + type: object + namespaceSelector: + description: "Selects Namespaces using cluster-scoped labels. This field follows standard label selector semantics; if present but empty, it selects all namespaces. \n If PodSelector is also set, then the NetworkPolicyPeer as a whole selects the Pods matching PodSelector in the Namespaces selected by NamespaceSelector. Otherwise it selects all Pods in the Namespaces selected by NamespaceSelector." + properties: + matchExpressions: + description: matchExpressions is a list of label selector requirements. The requirements are ANDed. + items: + description: A label selector requirement is a selector that contains values, a key, and an operator that relates the key and values. + properties: + key: + description: key is the label key that the selector applies to. + type: string + operator: + description: operator represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. + type: string + values: + description: values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch. + items: + type: string + type: array + required: + - key + - operator + type: object + type: array + matchLabels: + additionalProperties: + type: string + description: matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is "key", the operator is "In", and the values array contains only "value". The requirements are ANDed. + type: object + type: object + x-kubernetes-map-type: atomic + podSelector: + description: "This is a label selector which selects Pods. This field follows standard label selector semantics; if present but empty, it selects all pods. \n If NamespaceSelector is also set, then the NetworkPolicyPeer as a whole selects the Pods matching PodSelector in the Namespaces selected by NamespaceSelector. Otherwise it selects the Pods matching PodSelector in the policy's own Namespace." + properties: + matchExpressions: + description: matchExpressions is a list of label selector requirements. The requirements are ANDed. + items: + description: A label selector requirement is a selector that contains values, a key, and an operator that relates the key and values. + properties: + key: + description: key is the label key that the selector applies to. + type: string + operator: + description: operator represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. + type: string + values: + description: values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch. + items: + type: string + type: array + required: + - key + - operator + type: object + type: array + matchLabels: + additionalProperties: + type: string + description: matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is "key", the operator is "In", and the values array contains only "value". The requirements are ANDed. + type: object + type: object + x-kubernetes-map-type: atomic + type: object + type: array + ports: + description: List of ports which should be made accessible on the pods selected for this rule. Each item in this list is combined using a logical OR. If this field is empty or missing, this rule matches all ports (traffic not restricted by port). If this field is present and contains at least one item, then this rule allows traffic only if the traffic matches at least one port in the list. + items: + description: NetworkPolicyPort describes a port to allow traffic on + properties: + endPort: + description: If set, indicates that the range of ports from port to endPort, inclusive, should be allowed by the policy. This field cannot be defined if the port field is not defined or if the port field is defined as a named (string) port. The endPort must be equal or greater than port. + format: int32 + type: integer + port: + anyOf: + - type: integer + - type: string + description: The port on the given protocol. This can either be a numerical or named port on a pod. If this field is not provided, this matches all port names and numbers. If present, only traffic on the specified protocol AND port will be matched. + x-kubernetes-int-or-string: true + protocol: + default: TCP + description: The protocol (TCP, UDP, or SCTP) which traffic must match. If not specified, this field defaults to TCP. + type: string + type: object + type: array + type: object + type: array + podSelector: + description: Selects the pods to which this NetworkPolicy object applies. The array of ingress rules is applied to any pods selected by this field. Multiple network policies can select the same set of pods. In this case, the ingress rules for each are combined additively. This field is NOT optional and follows standard label selector semantics. An empty podSelector matches all pods in this namespace. + properties: + matchExpressions: + description: matchExpressions is a list of label selector requirements. The requirements are ANDed. + items: + description: A label selector requirement is a selector that contains values, a key, and an operator that relates the key and values. + properties: + key: + description: key is the label key that the selector applies to. + type: string + operator: + description: operator represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. + type: string + values: + description: values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch. + items: + type: string + type: array + required: + - key + - operator + type: object + type: array + matchLabels: + additionalProperties: + type: string + description: matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is "key", the operator is "In", and the values array contains only "value". The requirements are ANDed. + type: object + type: object + x-kubernetes-map-type: atomic + policyTypes: + description: List of rule types that the NetworkPolicy relates to. Valid options are ["Ingress"], ["Egress"], or ["Ingress", "Egress"]. If this field is not specified, it will default based on the existence of Ingress or Egress rules; policies that contain an Egress section are assumed to affect Egress, and all policies (whether or not they contain an Ingress section) are assumed to affect Ingress. If you want to write an egress-only policy, you must explicitly specify policyTypes [ "Egress" ]. Likewise, if you want to write a policy that specifies that no egress is allowed, you must specify a policyTypes value that include "Egress" (since such a policy would not include an Egress section and would otherwise default to just [ "Ingress" ]). This field is beta-level in 1.8 + items: + description: PolicyType string describes the NetworkPolicy type This type is beta-level in 1.8 + type: string + type: array + required: + - podSelector + type: object + type: array + type: object + resourceQuota: + description: Specifies a list of ResourceQuota resources assigned to the Space. The assigned values are inherited by the namespace created by the Space. Optional. + properties: + hard: + additionalProperties: + anyOf: + - type: integer + - type: string + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + description: 'hard is the set of desired hard limits for each named resource. More info: https://kubernetes.io/docs/concepts/policy/resource-quotas/' + type: object + scopeSelector: + description: scopeSelector is also a collection of filters like scopes that must match each object tracked by a quota but expressed using ScopeSelectorOperator in combination with possible values. For a resource to match, both scopes AND scopeSelector (if specified in spec), must be matched. + properties: + matchExpressions: + description: A list of scope selector requirements by scope of the resources. + items: + description: A scoped-resource selector requirement is a selector that contains values, a scope name, and an operator that relates the scope name and values. + properties: + operator: + description: Represents a scope's relationship to a set of values. Valid operators are In, NotIn, Exists, DoesNotExist. + type: string + scopeName: + description: The name of the scope that the selector applies to. + type: string + values: + description: An array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch. + items: + type: string + type: array + required: + - operator + - scopeName + type: object + type: array + type: object + x-kubernetes-map-type: atomic + scopes: + description: A collection of filters that must match each object tracked by a quota. If not specified, the quota matches all objects. + items: + description: A ResourceQuotaScope defines a filter that must match each object tracked by a quota + type: string + type: array + type: object type: object status: - description: SpaceStatus defines the observed state of Space. + description: SpaceTemplateStatus defines the observed state of SpaceTemplate. properties: conditions: description: Conditions List of status conditions to indicate the status of Space @@ -579,9 +1126,6 @@ spec: - type type: object type: array - namespaceName: - description: NamespaceName the name of the created underlying namespace. - type: string type: object type: object served: true @@ -739,6 +1283,32 @@ rules: - get - patch - update +- apiGroups: + - nauticus.io + resources: + - spacetemplates + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - nauticus.io + resources: + - spacetemplates/finalizers + verbs: + - update +- apiGroups: + - nauticus.io + resources: + - spacetemplates/status + verbs: + - get + - patch + - update - apiGroups: - networking.k8s.io resources: diff --git a/config/rbac/role.yaml b/config/rbac/role.yaml index 455a83f..aa16314 100644 --- a/config/rbac/role.yaml +++ b/config/rbac/role.yaml @@ -91,6 +91,32 @@ rules: - get - patch - update +- apiGroups: + - nauticus.io + resources: + - spacetemplates + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - nauticus.io + resources: + - spacetemplates/finalizers + verbs: + - update +- apiGroups: + - nauticus.io + resources: + - spacetemplates/status + verbs: + - get + - patch + - update - apiGroups: - networking.k8s.io resources: diff --git a/config/rbac/spacetemplate_editor_role.yaml b/config/rbac/spacetemplate_editor_role.yaml new file mode 100644 index 0000000..c0ae8ad --- /dev/null +++ b/config/rbac/spacetemplate_editor_role.yaml @@ -0,0 +1,31 @@ +# permissions for end users to edit spacetemplates. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/name: clusterrole + app.kubernetes.io/instance: spacetemplate-editor-role + app.kubernetes.io/component: rbac + app.kubernetes.io/created-by: nauticus + app.kubernetes.io/part-of: nauticus + app.kubernetes.io/managed-by: kustomize + name: spacetemplate-editor-role +rules: +- apiGroups: + - nauticus.io + resources: + - spacetemplates + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - nauticus.io + resources: + - spacetemplates/status + verbs: + - get diff --git a/config/rbac/spacetemplate_viewer_role.yaml b/config/rbac/spacetemplate_viewer_role.yaml new file mode 100644 index 0000000..37db5f9 --- /dev/null +++ b/config/rbac/spacetemplate_viewer_role.yaml @@ -0,0 +1,27 @@ +# permissions for end users to view spacetemplates. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/name: clusterrole + app.kubernetes.io/instance: spacetemplate-viewer-role + app.kubernetes.io/component: rbac + app.kubernetes.io/created-by: nauticus + app.kubernetes.io/part-of: nauticus + app.kubernetes.io/managed-by: kustomize + name: spacetemplate-viewer-role +rules: +- apiGroups: + - nauticus.io + resources: + - spacetemplates + verbs: + - get + - list + - watch +- apiGroups: + - nauticus.io + resources: + - spacetemplates/status + verbs: + - get diff --git a/config/samples/space_template_with_all.yaml b/config/samples/space_template_with_all.yaml new file mode 100644 index 0000000..ba702ac --- /dev/null +++ b/config/samples/space_template_with_all.yaml @@ -0,0 +1,74 @@ +apiVersion: nauticus.io/v1alpha1 +kind: SpaceTemplate +metadata: + labels: + app.kubernetes.io/name: spacetemplate + app.kubernetes.io/instance: spacetemplate-sample + app.kubernetes.io/part-of: nauticus + app.kubernetes.io/managed-by: kustomize + app.kubernetes.io/created-by: nauticus + name: space-tpl-sample +spec: + resourceQuota: + hard: + requests.cpu: "1" + requests.memory: "1Gi" + limits.cpu: "2" + limits.memory: "2Gi" + additionalRoleBindings: + - roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: viewer + subjects: + - name: alice + kind: User + networkPolicies: + enableDefaultStrictMode: true # false + items: + - policyTypes: + - Ingress + - Egress + egress: + - to: + - ipBlock: + cidr: 0.0.0.0/0 + except: + - 192.168.0.0/16 + ingress: + - from: + - namespaceSelector: + matchLabels: + app.kubernetes.io/instance: space-all-in-one + - podSelector: { } + - ipBlock: + cidr: 192.168.0.0/16 + podSelector: { } + limitRanges: + items: + - limits: + - max: + cpu: "1" + memory: 1Gi + min: + cpu: 50m + memory: 5Mi + type: Pod + - default: + cpu: 200m + memory: 100Mi + defaultRequest: + cpu: 100m + memory: 10Mi + max: + cpu: "1" + memory: 1Gi + min: + cpu: 50m + memory: 5Mi + type: Container + - max: + storage: 10Gi + min: + storage: 1Gi + type: PersistentVolumeClaim \ No newline at end of file diff --git a/config/samples/space_with_template_ref.yaml b/config/samples/space_with_template_ref.yaml new file mode 100644 index 0000000..abc5789 --- /dev/null +++ b/config/samples/space_with_template_ref.yaml @@ -0,0 +1,20 @@ +apiVersion: nauticus.io/v1alpha1 +kind: Space +metadata: + labels: + app.kubernetes.io/name: space + app.kubernetes.io/instance: space-sample + app.kubernetes.io/part-of: nauticus + app.kubernetes.io/managed-by: kustomize + app.kubernetes.io/created-by: nauticus + name: space-tpl-ref +spec: + templateRef: + group: nauticus.io/v1alpha1 + kind: SpaceTemplate # Specify the Kind of the referenced resource + name: space-tpl-sample # Name of the SpaceTemplate + owners: + - name: smile + kind: User + - name: smile@group.com + kind: Group \ No newline at end of file diff --git a/config/samples/space_with_tpl_merge.yaml b/config/samples/space_with_tpl_merge.yaml new file mode 100644 index 0000000..b32f977 --- /dev/null +++ b/config/samples/space_with_tpl_merge.yaml @@ -0,0 +1,35 @@ +apiVersion: nauticus.io/v1alpha1 +kind: Space +metadata: + labels: + app.kubernetes.io/name: space + app.kubernetes.io/instance: space-sample + app.kubernetes.io/part-of: nauticus + app.kubernetes.io/managed-by: kustomize + app.kubernetes.io/created-by: nauticus + name: space-tpl-ref-merge +spec: + templateRef: + group: nauticus.io/v1alpha1 + kind: SpaceTemplate # Specify the Kind of the referenced resource + name: space-tpl-sample # Name of the SpaceTemplate + owners: + - name: smile + kind: User + - name: smile@group.com + kind: Group + additionalRoleBindings: + - roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: viewer + subjects: + - name: dev + kind: Group + - roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: editor + subjects: + - name: ali + kind: User \ No newline at end of file diff --git a/config/samples/space_with_tpl_ref_overrides.yaml b/config/samples/space_with_tpl_ref_overrides.yaml new file mode 100644 index 0000000..7e731d5 --- /dev/null +++ b/config/samples/space_with_tpl_ref_overrides.yaml @@ -0,0 +1,26 @@ +apiVersion: nauticus.io/v1alpha1 +kind: Space +metadata: + labels: + app.kubernetes.io/name: space + app.kubernetes.io/instance: space-sample + app.kubernetes.io/part-of: nauticus + app.kubernetes.io/managed-by: kustomize + app.kubernetes.io/created-by: nauticus + name: space-tpl-ref-override +spec: + templateRef: + group: nauticus.io/v1alpha1 + kind: SpaceTemplate # Specify the Kind of the referenced resource + name: space-tpl-sample # Name of the SpaceTemplate + owners: + - name: smile + kind: User + - name: smile@group.com + kind: Group + resourceQuota: + hard: + limits.cpu: "20" + limits.memory: 24Gi + requests.cpu: "18" + requests.memory: 20Gi \ No newline at end of file diff --git a/controllers/reconciler.go b/controllers/reconciler.go deleted file mode 100644 index 7e04dfb..0000000 --- a/controllers/reconciler.go +++ /dev/null @@ -1,202 +0,0 @@ -// Copyright 2022-2023 Edixos -// SPDX-License-Identifier: Apache-2.0 - -package controllers - -import ( - "context" - "reflect" - "time" - - nauticusiov1alpha1 "github.com/edixos/nauticus/api/v1alpha1" - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" - "sigs.k8s.io/controller-runtime/pkg/reconcile" -) - -const ( - NauticusSpaceFinalizer = "nauticus.io/finalizer" - requeueAfter = time.Minute * 3 - - SpaceConditionReady nauticusiov1alpha1.ConditionType = "Ready" - SpaceConditionCreating nauticusiov1alpha1.ConditionType = "Creating" - SpaceConditionFailed nauticusiov1alpha1.ConditionType = "Failed" - - SpaceConditionStatusUnknown = metav1.ConditionStatus(corev1.ConditionUnknown) - SpaceConditionStatusTrue = metav1.ConditionStatus(corev1.ConditionTrue) - SpaceConditionStatusFalse = metav1.ConditionStatus(corev1.ConditionFalse) - - SpaceSyncSuccessReason nauticusiov1alpha1.ConditionReason = "SpaceSyncedSuccessfully" - SpaceCreatingReason nauticusiov1alpha1.ConditionReason = "SpaceCreating" - SpaceFailedReason nauticusiov1alpha1.ConditionReason = "SpaceSyncFailed" - - SpaceSyncSuccessMessage nauticusiov1alpha1.ConditionMessage = "Space synced successfully" - SpaceSyncFailMessage nauticusiov1alpha1.ConditionMessage = "Space failed to sync" - SpaceCreatingMessage nauticusiov1alpha1.ConditionMessage = "Creating Space in progress" -) - -func (s *SpaceReconciler) reconcileSpace(ctx context.Context, space *nauticusiov1alpha1.Space) (result reconcile.Result, err error) { - if !controllerutil.ContainsFinalizer(space, NauticusSpaceFinalizer) { - controllerutil.AddFinalizer(space, NauticusSpaceFinalizer) - - if err = s.Update(ctx, space); err != nil { - return ctrl.Result{}, err - } - } - - s.Log.Info("Reconciling Namespace for space.") - - s.processInProgressCondition(ctx, space) - s.setMetrics(space, SpaceConditionCreating) - - err = s.reconcileNamespace(ctx, space) - if err != nil { - s.processFailedCondition(ctx, space) - s.setMetrics(space, SpaceConditionFailed) - - return ctrl.Result{}, err - } - - resourceQuotaSpecValue := reflect.ValueOf(space.Spec.ResourceQuota) - if !resourceQuotaSpecValue.IsZero() { - s.Log.Info("Reconciling Resource Quota for space") - err = s.reconcileResourceQuota(ctx, space) - - if err != nil { - s.processFailedCondition(ctx, space) - s.setMetrics(space, SpaceConditionFailed) - - return ctrl.Result{}, err - } - } - - ownerRoleBindingSpecValue := reflect.ValueOf(space.Spec.Owners) - if !ownerRoleBindingSpecValue.IsZero() { - s.Log.Info("Reconciling Owner Role Binding for space") - err = s.reconcileOwners(ctx, space) - - if err != nil { - s.processFailedCondition(ctx, space) - s.setMetrics(space, SpaceConditionFailed) - - return ctrl.Result{}, err - } - } - - additionalBindingSpecValue := reflect.ValueOf(space.Spec.AdditionalRoleBindings) - if !additionalBindingSpecValue.IsZero() { - s.Log.Info("Reconciling Additional Role Binding for space") - err = s.reconcileAdditionalRoleBindings(ctx, space) - - if err != nil { - s.processFailedCondition(ctx, space) - s.setMetrics(space, SpaceConditionFailed) - - return ctrl.Result{}, err - } - } - - networkPolicies := reflect.ValueOf(space.Spec.NetworkPolicies) - if !networkPolicies.IsZero() { - s.Log.Info("Reconciling NetworkPolicies for space") - err = s.reconcileNetworkPolicies(ctx, space) - - if err != nil { - s.processFailedCondition(ctx, space) - s.setMetrics(space, SpaceConditionFailed) - - return ctrl.Result{}, err - } - } - - limitRanges := reflect.ValueOf(space.Spec.LimitRanges) - if !limitRanges.IsZero() { - s.Log.Info("Reconciling LimitRanges for space") - err = s.reconcileLimitRanges(ctx, space) - - if err != nil { - s.processFailedCondition(ctx, space) - s.setMetrics(space, SpaceConditionFailed) - - return ctrl.Result{}, err - } - } - - serviceAccounts := reflect.ValueOf(space.Spec.ServiceAccounts) - if !serviceAccounts.IsZero() { - s.Log.Info("Reconciling ServiceAccounts for space") - err = s.reconcileServiceAccounts(ctx, space) - - if err != nil { - s.processFailedCondition(ctx, space) - s.setMetrics(space, SpaceConditionFailed) - - return ctrl.Result{}, err - } - } - - s.processReadyCondition(ctx, space) - s.setMetrics(space, SpaceConditionReady) - - return ctrl.Result{ - RequeueAfter: requeueAfter, - }, nil -} - -func (s *SpaceReconciler) reconcileDelete(ctx context.Context, space *nauticusiov1alpha1.Space) (result reconcile.Result, err error) { - // The annotation is set, so skip namespace deletion - // Just remove the finalizer from the Space - if space.HasIgnoreUnderlyingDeletionAnnotation() { - if controllerutil.ContainsFinalizer(space, NauticusSpaceFinalizer) { - controllerutil.RemoveFinalizer(space, NauticusSpaceFinalizer) - - if err = s.Update(ctx, space); err != nil { - return ctrl.Result{}, err - } - } - - return ctrl.Result{}, err - } - // If the annotation is not set, delete all created resources - if controllerutil.ContainsFinalizer(space, NauticusSpaceFinalizer) { - if err = s.deleteNetworkPolicies(ctx, space); client.IgnoreNotFound(err) != nil { - return ctrl.Result{}, err - } - - if err = s.deleteLimitRanges(ctx, space); client.IgnoreNotFound(err) != nil { - return ctrl.Result{}, err - } - - if err = s.deleteOwners(ctx, space); client.IgnoreNotFound(err) != nil { - return ctrl.Result{}, err - } - - if err = s.deleteAdditionalRoleBindings(ctx, space); client.IgnoreNotFound(err) != nil { - return ctrl.Result{}, err - } - - if err = s.deleteResourceQuota(ctx, space); client.IgnoreNotFound(err) != nil { - return ctrl.Result{}, err - } - - if err = s.deleteServiceAccounts(ctx, space); client.IgnoreNotFound(err) != nil { - return ctrl.Result{}, err - } - - if err = s.deleteNamespace(ctx, space); client.IgnoreNotFound(err) != nil { - return ctrl.Result{}, err - } - - // remove our finalizer from the list and update it. - controllerutil.RemoveFinalizer(space, NauticusSpaceFinalizer) - - if err = s.Update(ctx, space); err != nil { - return ctrl.Result{}, err - } - } - // Stop reconciliation as the item is being deleted - return ctrl.Result{}, err -} diff --git a/controllers/shared/reconsiler.go b/controllers/shared/reconsiler.go new file mode 100644 index 0000000..569736b --- /dev/null +++ b/controllers/shared/reconsiler.go @@ -0,0 +1,107 @@ +// Copyright 2022-2023 Edixos +// SPDX-License-Identifier: Apache-2.0 +package shared + +import ( + "context" + + "github.com/go-logr/logr" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/tools/record" + "k8s.io/utils/clock" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" +) + +// Reconciler reconciles an object. +type Reconciler struct { + client.Client + Scheme *runtime.Scheme + Recorder record.EventRecorder + Log logr.Logger +} + +// Clock is defined as a package var so it can be stubbed out during tests. +var Clock clock.Clock = clock.RealClock{} + +// ConditionedObject is an interface that describes condition-related operations. +type ConditionedObject interface { + client.Object + metav1.Object + GetConditions() []metav1.Condition + SetConditions([]metav1.Condition) +} + +func (r *Reconciler) EmitEvent(object runtime.Object, name string, res controllerutil.OperationResult, msg string, err error) { + eventType := corev1.EventTypeNormal + + if err != nil { + eventType = corev1.EventTypeWarning + res = "Error" + } + + r.Recorder.AnnotatedEventf(object, map[string]string{"OperationResult": string(res)}, eventType, name, msg) +} + +func (r *Reconciler) setCondition(object ConditionedObject, observedGeneration int64, conditionType string, status metav1.ConditionStatus, reason, message string) { + newCondition := metav1.Condition{ + Type: conditionType, + Status: status, + Reason: reason, + Message: message, + } + nowTime := metav1.NewTime(Clock.Now()) + newCondition.LastTransitionTime = nowTime + + // Set the condition generation + newCondition.ObservedGeneration = observedGeneration + + // Search through existing conditions + existingConditions := object.GetConditions() + for idx, cond := range existingConditions { + // Skip unrelated conditions + if cond.Type != conditionType { + continue + } + + // If this update doesn't contain a state transition, we don't update + // the conditions LastTransitionTime to Now() + if cond.Status == status { + newCondition.LastTransitionTime = cond.LastTransitionTime + } else { + r.Log.WithName(object.GetName()).Info("Found status change for Space condition, setting lastTransitionTime to", object.GetName(), nowTime) + } + + // Overwrite the existing condition + existingConditions[idx] = newCondition + + return + } + + // If we've not found an existing condition of this type, we simply insert + // the new condition into the slice. + object.SetConditions(append(existingConditions, newCondition)) + r.Log.WithName(object.GetName()).Info("Setting lastTransitionTime condition for ", "name", object.GetName(), "time", newCondition.LastTransitionTime.Time) +} + +func (r *Reconciler) ProcessCondition(ctx context.Context, object ConditionedObject, conditionType string, status metav1.ConditionStatus, reason, message string) { + r.setCondition(object, object.GetGeneration(), conditionType, status, reason, message) + + err := r.UpdateStatus(ctx, object) + if err != nil { + return + } +} + +func (r *Reconciler) UpdateStatus(ctx context.Context, object client.Object) (err error) { + err = r.Client.Status().Update(ctx, object) + if err != nil { + r.Log.Info("Failed to update status", "object", object.GetName()) + + return err + } + + return nil +} diff --git a/controllers/shared/utlis.go b/controllers/shared/utlis.go new file mode 100644 index 0000000..4952fe2 --- /dev/null +++ b/controllers/shared/utlis.go @@ -0,0 +1,44 @@ +// Copyright 2022-2023 Edixos +// SPDX-License-Identifier: Apache-2.0 + +package shared + +import ( + "context" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "sigs.k8s.io/controller-runtime/pkg/client" +) + +func (r *Reconciler) DeleteObject(ctx context.Context, object client.Object) (err error) { + if err = r.Client.Delete(ctx, object); client.IgnoreNotFound(err) != nil { + return err + } + + return nil +} + +func (r *Reconciler) ProcessFailedCondition(ctx context.Context, object ConditionedObject, conditionType string, status metav1.ConditionStatus, reason, message string) { + r.setCondition(object, object.GetGeneration(), conditionType, status, reason, message) + err := r.UpdateStatus(ctx, object) + if err != nil { //nolint + return + } +} + +func (r *Reconciler) ProcessReadyCondition(ctx context.Context, object ConditionedObject, conditionType string, status metav1.ConditionStatus, reason, message string) { + r.setCondition(object, object.GetGeneration(), conditionType, status, reason, message) + err := r.UpdateStatus(ctx, object) + if err != nil { //nolint + return + } +} + +func (r *Reconciler) ProcessInProgressCondition(ctx context.Context, object ConditionedObject, conditionType string, status metav1.ConditionStatus, reason, message string) { + r.setCondition(object, object.GetGeneration(), conditionType, status, reason, message) + err := r.UpdateStatus(ctx, object) + if err != nil { //nolint + return + } +} diff --git a/controllers/space_controller.go b/controllers/space/controller.go similarity index 81% rename from controllers/space_controller.go rename to controllers/space/controller.go index 6be1807..63a88cb 100644 --- a/controllers/space_controller.go +++ b/controllers/space/controller.go @@ -1,15 +1,12 @@ // Copyright 2022-2023 Edixos // SPDX-License-Identifier: Apache-2.0 -package controllers +package space import ( "context" - "github.com/go-logr/logr" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/client-go/tools/record" - "sigs.k8s.io/controller-runtime/pkg/client" + "github.com/edixos/nauticus/controllers/shared" "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/predicate" @@ -18,12 +15,9 @@ import ( ctrl "sigs.k8s.io/controller-runtime" ) -// SpaceReconciler reconciles a Space object. -type SpaceReconciler struct { - client.Client - Scheme *runtime.Scheme - Recorder record.EventRecorder - Log logr.Logger +// Reconciler reconciles a Space object. +type Reconciler struct { + shared.Reconciler } //+kubebuilder:rbac:groups=nauticus.io,resources=spaces,verbs=get;list;watch;create;update;patch;delete @@ -46,13 +40,13 @@ type SpaceReconciler struct { // // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.13.1/pkg/reconcile -func (s *SpaceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - log := s.Log.WithValues("space", req.NamespacedName) +func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + log := r.Log.WithValues("space", req.NamespacedName) // Fetch the Space instance space := &nauticusiov1alpha1.Space{} - err := s.Get(ctx, req.NamespacedName, space) + err := r.Get(ctx, req.NamespacedName, space) if err != nil { if apierrors.IsNotFound(err) { // Space not found, return @@ -66,19 +60,23 @@ func (s *SpaceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl } if !space.ObjectMeta.DeletionTimestamp.IsZero() { - return s.reconcileDelete(ctx, space) + return r.reconcileDelete(ctx, space) } - return s.reconcileSpace(ctx, space) + if space.Spec.TemplateRef.Name != "" { + return r.reconcileSpaceFromTemplate(ctx, space) + } + + return r.reconcileSpace(ctx, space) } // SetupWithManager sets up the controller with the Manager. -func (s *SpaceReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&nauticusiov1alpha1.Space{}). WithEventFilter(predicate.GenerationChangedPredicate{}). WithEventFilter(ignoreDeletionPredicate()). - Complete(s) + Complete(r) } func ignoreDeletionPredicate() predicate.Predicate { diff --git a/controllers/limit_range.go b/controllers/space/limit_range.go similarity index 67% rename from controllers/limit_range.go rename to controllers/space/limit_range.go index cae89e9..ca12d74 100644 --- a/controllers/limit_range.go +++ b/controllers/space/limit_range.go @@ -1,7 +1,7 @@ // Copyright 2022-2023 Edixos // SPDX-License-Identifier: Apache-2.0 -package controllers +package space import ( "context" @@ -14,14 +14,14 @@ import ( "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" ) -func (s *SpaceReconciler) reconcileLimitRanges(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { +func (r *Reconciler) reconcileLimitRanges(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { for i, limitRange := range space.Spec.LimitRanges.Items { lrName := "nauticus-custom-" + strconv.Itoa(i) lr := newLimitRange(lrName, space.Status.NamespaceName, limitRange) - err = s.syncLimitRange(ctx, lr, space, limitRange) + err = r.syncLimitRange(ctx, lr, space, limitRange) if err != nil { - s.Log.Error(err, "Cannot Synchronize Limit Range") + r.Log.Error(err, "Cannot Synchronize Limit Range") return err } @@ -30,7 +30,7 @@ func (s *SpaceReconciler) reconcileLimitRanges(ctx context.Context, space *nauti return nil } -func (s *SpaceReconciler) syncLimitRange(ctx context.Context, limitRange *corev1.LimitRange, space *nauticusiov1alpha1.Space, spec corev1.LimitRangeSpec) (err error) { +func (r *Reconciler) syncLimitRange(ctx context.Context, limitRange *corev1.LimitRange, space *nauticusiov1alpha1.Space, spec corev1.LimitRangeSpec) (err error) { var ( res controllerutil.OperationResult spaceLabel, limitRangeLabel string @@ -44,7 +44,7 @@ func (s *SpaceReconciler) syncLimitRange(ctx context.Context, limitRange *corev1 return } - res, err = controllerutil.CreateOrUpdate(ctx, s.Client, limitRange, func() (err error) { + res, err = controllerutil.CreateOrUpdate(ctx, r.Client, limitRange, func() (err error) { limitRange.SetLabels(map[string]string{ spaceLabel: space.Name, limitRangeLabel: limitRange.Name, @@ -53,8 +53,8 @@ func (s *SpaceReconciler) syncLimitRange(ctx context.Context, limitRange *corev1 return nil }) - s.Log.Info("LimitRange sync result: "+string(res), "name", limitRange.Name, "namespace", space.Status.NamespaceName) - s.emitEvent(space, space.Name, res, "Ensuring LimitRange creation/Update", err) + r.Log.Info("LimitRange sync result: "+string(res), "name", limitRange.Name, "namespace", space.Status.NamespaceName) + r.EmitEvent(space, space.Name, res, "Ensuring LimitRange creation/Update", err) return nil } @@ -69,12 +69,12 @@ func newLimitRange(name, namespace string, limitRangeSpec corev1.LimitRangeSpec) } } -func (s *SpaceReconciler) deleteLimitRanges(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { +func (r *Reconciler) deleteLimitRanges(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { for i, limitRange := range space.Spec.LimitRanges.Items { lrName := "nauticus-custom-" + strconv.Itoa(i) lr := newLimitRange(lrName, space.Status.NamespaceName, limitRange) - if err = s.deleteObject(ctx, lr); err != nil { + if err = r.DeleteObject(ctx, lr); err != nil { return err } } diff --git a/controllers/space/metrics.go b/controllers/space/metrics.go new file mode 100644 index 0000000..ef1ba04 --- /dev/null +++ b/controllers/space/metrics.go @@ -0,0 +1,27 @@ +// Copyright 2022-2023 Edixos +// SPDX-License-Identifier: Apache-2.0 + +package space + +import ( + "github.com/edixos/nauticus/api/v1alpha1" + "github.com/edixos/nauticus/pkg/controller/constants" + "github.com/edixos/nauticus/pkg/metrics" +) + +func (r *Reconciler) setMetrics(space *v1alpha1.Space, conditionType v1alpha1.ConditionType) { + switch conditionType { + case v1alpha1.ConditionType(constants.SpaceConditionCreating): + metrics.ReadySpaces.WithLabelValues(space.Name).Set(0) + metrics.InProgressSpaces.WithLabelValues(space.Name).Set(1) + metrics.FailedSpaces.WithLabelValues(space.Name).Set(0) + case v1alpha1.ConditionType(constants.SpaceConditionReady): + metrics.ReadySpaces.WithLabelValues(space.Name).Set(1) + metrics.InProgressSpaces.WithLabelValues(space.Name).Set(0) + metrics.FailedSpaces.WithLabelValues(space.Name).Set(0) + case v1alpha1.ConditionType(constants.SpaceConditionFailed): + metrics.ReadySpaces.WithLabelValues(space.Name).Set(0) + metrics.InProgressSpaces.WithLabelValues(space.Name).Set(0) + metrics.FailedSpaces.WithLabelValues(space.Name).Set(1) + } +} diff --git a/controllers/namespace.go b/controllers/space/namespace.go similarity index 51% rename from controllers/namespace.go rename to controllers/space/namespace.go index b181ea7..1cfd1fc 100644 --- a/controllers/namespace.go +++ b/controllers/space/namespace.go @@ -1,7 +1,7 @@ // Copyright 2022-2023 Edixos // SPDX-License-Identifier: Apache-2.0 -package controllers +package space import ( "context" @@ -13,10 +13,10 @@ import ( "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" ) -func (s *SpaceReconciler) reconcileNamespace(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { - namespace := s.newNamespace(space) +func (r *Reconciler) reconcileNamespace(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { + namespace := r.newNamespace(space) - err = s.syncNamespace(ctx, namespace, space) + err = r.syncNamespace(ctx, namespace, space) if err != nil { return err @@ -24,12 +24,16 @@ func (s *SpaceReconciler) reconcileNamespace(ctx context.Context, space *nauticu // Update the Space's status space.Status.NamespaceName = namespace.Name - s.updateStatus(ctx, space) + err = r.UpdateStatus(ctx, space) + + if err != nil { + r.Log.Info("error updating the status") + } return err } -func (s *SpaceReconciler) syncNamespace(ctx context.Context, namespace *corev1.Namespace, space *nauticusiov1alpha1.Space) (err error) { +func (r *Reconciler) syncNamespace(ctx context.Context, namespace *corev1.Namespace, space *nauticusiov1alpha1.Space) (err error) { var ( res controllerutil.OperationResult spaceLabel, namespaceLabel string @@ -43,7 +47,7 @@ func (s *SpaceReconciler) syncNamespace(ctx context.Context, namespace *corev1.N return } - res, err = controllerutil.CreateOrUpdate(ctx, s.Client, namespace, func() error { + res, err = controllerutil.CreateOrUpdate(ctx, r.Client, namespace, func() error { namespace.SetLabels(map[string]string{ spaceLabel: space.Name, namespaceLabel: namespace.Name, @@ -51,16 +55,16 @@ func (s *SpaceReconciler) syncNamespace(ctx context.Context, namespace *corev1.N return nil }) - s.Log.Info("Namespace sync result: "+string(res), "name", namespace.Name) - s.emitEvent(space, space.Name, res, "Ensuring Namespace creation", err) + r.Log.Info("Namespace sync result: "+string(res), "name", namespace.Name) + r.EmitEvent(space, space.Name, res, "Ensuring Namespace creation", err) return err } -func (s *SpaceReconciler) newNamespace(space *nauticusiov1alpha1.Space) *corev1.Namespace { +func (r *Reconciler) newNamespace(space *nauticusiov1alpha1.Space) *corev1.Namespace { namespace := &corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ - Name: s.namespaceName(space), + Name: r.namespaceName(space), Labels: space.Labels, }, } @@ -68,12 +72,12 @@ func (s *SpaceReconciler) newNamespace(space *nauticusiov1alpha1.Space) *corev1. return namespace } -func (s *SpaceReconciler) namespaceName(space *nauticusiov1alpha1.Space) string { +func (r *Reconciler) namespaceName(space *nauticusiov1alpha1.Space) string { return space.Name } -func (s *SpaceReconciler) deleteNamespace(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { - namespace := s.newNamespace(space) +func (r *Reconciler) deleteNamespace(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { + namespace := r.newNamespace(space) - return s.deleteObject(ctx, namespace) + return r.DeleteObject(ctx, namespace) } diff --git a/controllers/network_policy.go b/controllers/space/network_policy.go similarity index 75% rename from controllers/network_policy.go rename to controllers/space/network_policy.go index 111ff62..672219f 100644 --- a/controllers/network_policy.go +++ b/controllers/space/network_policy.go @@ -1,7 +1,7 @@ // Copyright 2022-2023 Edixos // SPDX-License-Identifier: Apache-2.0 -package controllers +package space import ( "context" @@ -15,15 +15,15 @@ import ( "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" ) -func (s *SpaceReconciler) reconcileNetworkPolicies(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { +func (r *Reconciler) reconcileNetworkPolicies(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { if space.Spec.NetworkPolicies.EnableDefaultStrictMode { networkPolicyName := fmt.Sprintf("nauticus-%s", space.Name) networkPolicySpec := newNetworkPolicyDefaultSpec() networkPolicy := newNetworkPolicy(networkPolicyName, space.Status.NamespaceName, networkPolicySpec) - err = s.syncNetworkPolicy(ctx, networkPolicy, space, networkPolicySpec) + err = r.syncNetworkPolicy(ctx, networkPolicy, space, networkPolicySpec) if err != nil { - s.Log.Error(err, "Cannot Synchronize Network policy") + r.Log.Error(err, "Cannot Synchronize Network policy") return err } @@ -32,10 +32,10 @@ func (s *SpaceReconciler) reconcileNetworkPolicies(ctx context.Context, space *n for i, networkPolicy := range space.Spec.NetworkPolicies.Items { npName := "nauticus-custom-" + strconv.Itoa(i) np := newNetworkPolicy(npName, space.Status.NamespaceName, networkPolicy) - err = s.syncNetworkPolicy(ctx, np, space, networkPolicy) + err = r.syncNetworkPolicy(ctx, np, space, networkPolicy) if err != nil { - s.Log.Error(err, "Cannot Synchronize Network policy") + r.Log.Error(err, "Cannot Synchronize Network policy") return err } @@ -44,7 +44,7 @@ func (s *SpaceReconciler) reconcileNetworkPolicies(ctx context.Context, space *n return nil } -func (s *SpaceReconciler) syncNetworkPolicy(ctx context.Context, networkPolicy *networkingv1.NetworkPolicy, space *nauticusiov1alpha1.Space, spec networkingv1.NetworkPolicySpec) (err error) { +func (r *Reconciler) syncNetworkPolicy(ctx context.Context, networkPolicy *networkingv1.NetworkPolicy, space *nauticusiov1alpha1.Space, spec networkingv1.NetworkPolicySpec) (err error) { var ( res controllerutil.OperationResult spaceLabel, networkPolicyLabel string @@ -58,7 +58,7 @@ func (s *SpaceReconciler) syncNetworkPolicy(ctx context.Context, networkPolicy * return } - res, err = controllerutil.CreateOrUpdate(ctx, s.Client, networkPolicy, func() (err error) { + res, err = controllerutil.CreateOrUpdate(ctx, r.Client, networkPolicy, func() (err error) { networkPolicy.SetLabels(map[string]string{ spaceLabel: space.Name, networkPolicyLabel: networkPolicy.Name, @@ -69,8 +69,8 @@ func (s *SpaceReconciler) syncNetworkPolicy(ctx context.Context, networkPolicy * return nil }) - s.Log.Info("Network Policy sync result: "+string(res), "name", networkPolicy.Name, "namespace", space.Status.NamespaceName) - s.emitEvent(space, space.Name, res, "Ensuring NetworkPolicy creation/Update", err) + r.Log.Info("Network Policy sync result: "+string(res), "name", networkPolicy.Name, "namespace", space.Status.NamespaceName) + r.EmitEvent(space, space.Name, res, "Ensuring NetworkPolicy creation/Update", err) return nil } @@ -109,13 +109,13 @@ func newNetworkPolicyDefaultSpec() networkingv1.NetworkPolicySpec { } } -func (s *SpaceReconciler) deleteNetworkPolicies(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { +func (r *Reconciler) deleteNetworkPolicies(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { if space.Spec.NetworkPolicies.EnableDefaultStrictMode { networkPolicyName := fmt.Sprintf("nauticus-%s", space.Name) networkPolicySpec := newNetworkPolicyDefaultSpec() networkPolicy := newNetworkPolicy(networkPolicyName, space.Status.NamespaceName, networkPolicySpec) - if err = s.deleteObject(ctx, networkPolicy); err != nil { + if err = r.DeleteObject(ctx, networkPolicy); err != nil { return err } } @@ -124,7 +124,7 @@ func (s *SpaceReconciler) deleteNetworkPolicies(ctx context.Context, space *naut npName := "nauticus-custom-" + strconv.Itoa(i) np := newNetworkPolicy(npName, space.Status.NamespaceName, networkPolicy) - if err = s.deleteObject(ctx, np); err != nil { + if err = r.DeleteObject(ctx, np); err != nil { return err } } diff --git a/controllers/rbac.go b/controllers/space/rbac.go similarity index 67% rename from controllers/rbac.go rename to controllers/space/rbac.go index 1a43c40..195e0dd 100644 --- a/controllers/rbac.go +++ b/controllers/space/rbac.go @@ -1,7 +1,7 @@ // Copyright 2022-2023 Edixos // SPDX-License-Identifier: Apache-2.0 -package controllers +package space import ( "context" @@ -13,7 +13,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" ) -func (s *SpaceReconciler) reconcileOwners(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { +func (r *Reconciler) reconcileOwners(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { rolebindingName := space.Name + "-owner" roleRef := rbacv1.RoleRef{ @@ -23,23 +23,23 @@ func (s *SpaceReconciler) reconcileOwners(ctx context.Context, space *nauticusio } ownersRoleBinding := newRoleBinding(rolebindingName, space.Status.NamespaceName, roleRef, space.Spec.Owners) - err = s.syncRoleBinding(ctx, ownersRoleBinding, space, ownersRoleBinding.RoleRef, ownersRoleBinding.Subjects) + err = r.syncRoleBinding(ctx, ownersRoleBinding, space, ownersRoleBinding.RoleRef, ownersRoleBinding.Subjects) return err } -func (s *SpaceReconciler) reconcileAdditionalRoleBindings(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { +func (r *Reconciler) reconcileAdditionalRoleBindings(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { for _, ad := range space.Spec.AdditionalRoleBindings { rolebindingName := space.Name + "-" + ad.RoleRef.Name additionalRoleBinding := newRoleBinding(rolebindingName, space.Status.NamespaceName, ad.RoleRef, ad.Subjects) - err = s.syncRoleBinding(ctx, additionalRoleBinding, space, ad.RoleRef, ad.Subjects) + err = r.syncRoleBinding(ctx, additionalRoleBinding, space, ad.RoleRef, ad.Subjects) } return err } -func (s *SpaceReconciler) syncRoleBinding(ctx context.Context, roleBinding *rbacv1.RoleBinding, space *nauticusiov1alpha1.Space, desiredRoleRef rbacv1.RoleRef, desiredSubjects []rbacv1.Subject) (err error) { +func (r *Reconciler) syncRoleBinding(ctx context.Context, roleBinding *rbacv1.RoleBinding, space *nauticusiov1alpha1.Space, desiredRoleRef rbacv1.RoleRef, desiredSubjects []rbacv1.Subject) (err error) { var ( res controllerutil.OperationResult spaceLabel, roleBindingLabel string @@ -53,7 +53,7 @@ func (s *SpaceReconciler) syncRoleBinding(ctx context.Context, roleBinding *rbac return } - res, err = controllerutil.CreateOrUpdate(ctx, s.Client, roleBinding, func() error { + res, err = controllerutil.CreateOrUpdate(ctx, r.Client, roleBinding, func() error { roleBinding.SetLabels(map[string]string{ spaceLabel: space.Name, roleBindingLabel: roleBinding.Name, @@ -64,8 +64,8 @@ func (s *SpaceReconciler) syncRoleBinding(ctx context.Context, roleBinding *rbac return nil }) - s.Log.Info("Rolebinding sync result: "+string(res), "name", roleBinding.Name, "namespace", space.Status.NamespaceName) - s.emitEvent(space, space.Name, res, "Ensuring RoleBinding creation/Update", err) + r.Log.Info("Rolebinding sync result: "+string(res), "name", roleBinding.Name, "namespace", space.Status.NamespaceName) + r.EmitEvent(space, space.Name, res, "Ensuring RoleBinding creation/Update", err) return err } @@ -81,23 +81,23 @@ func newRoleBinding(name string, namespace string, roleRef rbacv1.RoleRef, subje } } -func (s *SpaceReconciler) deleteOwners(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { +func (r *Reconciler) deleteOwners(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { rolebindingName := space.Name + "-owner" roleRef := rbacv1.RoleRef{} ownersRoleBinding := newRoleBinding(rolebindingName, space.Status.NamespaceName, roleRef, space.Spec.Owners) - err = s.deleteObject(ctx, ownersRoleBinding) + err = r.DeleteObject(ctx, ownersRoleBinding) return err } -func (s *SpaceReconciler) deleteAdditionalRoleBindings(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { +func (r *Reconciler) deleteAdditionalRoleBindings(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { for _, ad := range space.Spec.AdditionalRoleBindings { rolebindingName := space.Name + "-" + ad.RoleRef.Name additionalRoleBinding := newRoleBinding(rolebindingName, space.Status.NamespaceName, ad.RoleRef, ad.Subjects) - err = s.deleteObject(ctx, additionalRoleBinding) + err = r.DeleteObject(ctx, additionalRoleBinding) } return err diff --git a/controllers/space/reconciler.go b/controllers/space/reconciler.go new file mode 100644 index 0000000..ab92b6f --- /dev/null +++ b/controllers/space/reconciler.go @@ -0,0 +1,214 @@ +// Copyright 2022-2023 Edixos +// SPDX-License-Identifier: Apache-2.0 + +package space + +import ( + "context" + "reflect" + + nauticusiov1alpha1 "github.com/edixos/nauticus/api/v1alpha1" + "github.com/edixos/nauticus/pkg/controller/constants" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + "sigs.k8s.io/controller-runtime/pkg/reconcile" +) + +func (r *Reconciler) reconcileSpace(ctx context.Context, space *nauticusiov1alpha1.Space) (result reconcile.Result, err error) { + if !controllerutil.ContainsFinalizer(space, constants.NauticusSpaceFinalizer) { + controllerutil.AddFinalizer(space, constants.NauticusSpaceFinalizer) + + if err = r.Update(ctx, space); err != nil { + return ctrl.Result{}, err + } + } + + r.Log.Info("Reconciling Namespace for space.") + + r.ProcessInProgressCondition(ctx, space, constants.SpaceConditionCreating, metav1.ConditionUnknown, constants.SpaceCreatingReason, constants.SpaceCreatingMessage) + r.setMetrics(space, nauticusiov1alpha1.ConditionType(constants.SpaceConditionCreating)) + + err = r.reconcileNamespace(ctx, space) + if err != nil { + r.ProcessFailedCondition(ctx, space, constants.SpaceConditionFailed, metav1.ConditionFalse, constants.SpaceFailedReason, constants.SpaceSyncFailMessage) + r.setMetrics(space, nauticusiov1alpha1.ConditionType(constants.SpaceConditionFailed)) + + return ctrl.Result{}, err + } + + resourceQuotaSpecValue := reflect.ValueOf(space.Spec.ResourceQuota) + if !resourceQuotaSpecValue.IsZero() { + r.Log.Info("Reconciling Resource Quota for space") + err = r.reconcileResourceQuota(ctx, space) + + if err != nil { + r.ProcessFailedCondition(ctx, space, constants.SpaceConditionFailed, metav1.ConditionFalse, constants.SpaceFailedReason, constants.SpaceSyncFailMessage) + r.setMetrics(space, nauticusiov1alpha1.ConditionType(constants.SpaceConditionFailed)) + + return ctrl.Result{}, err + } + } + + ownerRoleBindingSpecValue := reflect.ValueOf(space.Spec.Owners) + if !ownerRoleBindingSpecValue.IsZero() { + r.Log.Info("Reconciling Owner Role Binding for space") + err = r.reconcileOwners(ctx, space) + + if err != nil { + r.ProcessFailedCondition(ctx, space, constants.SpaceConditionFailed, metav1.ConditionFalse, constants.SpaceFailedReason, constants.SpaceSyncFailMessage) + r.setMetrics(space, nauticusiov1alpha1.ConditionType(constants.SpaceConditionFailed)) + + return ctrl.Result{}, err + } + } + + additionalBindingSpecValue := reflect.ValueOf(space.Spec.AdditionalRoleBindings) + if !additionalBindingSpecValue.IsZero() { + r.Log.Info("Reconciling Additional Role Binding for space") + err = r.reconcileAdditionalRoleBindings(ctx, space) + + if err != nil { + r.ProcessFailedCondition(ctx, space, constants.SpaceConditionFailed, metav1.ConditionFalse, constants.SpaceFailedReason, constants.SpaceSyncFailMessage) + r.setMetrics(space, nauticusiov1alpha1.ConditionType(constants.SpaceConditionFailed)) + + return ctrl.Result{}, err + } + } + + networkPolicies := reflect.ValueOf(space.Spec.NetworkPolicies) + if !networkPolicies.IsZero() { + r.Log.Info("Reconciling NetworkPolicies for space") + err = r.reconcileNetworkPolicies(ctx, space) + + if err != nil { + r.ProcessFailedCondition(ctx, space, constants.SpaceConditionFailed, metav1.ConditionFalse, constants.SpaceFailedReason, constants.SpaceSyncFailMessage) + r.setMetrics(space, nauticusiov1alpha1.ConditionType(constants.SpaceConditionFailed)) + + return ctrl.Result{}, err + } + } + + limitRanges := reflect.ValueOf(space.Spec.LimitRanges) + if !limitRanges.IsZero() { + r.Log.Info("Reconciling LimitRanges for space") + err = r.reconcileLimitRanges(ctx, space) + + if err != nil { + r.ProcessFailedCondition(ctx, space, constants.SpaceConditionFailed, metav1.ConditionFalse, constants.SpaceFailedReason, constants.SpaceSyncFailMessage) + r.setMetrics(space, nauticusiov1alpha1.ConditionType(constants.SpaceConditionFailed)) + + return ctrl.Result{}, err + } + } + + serviceAccounts := reflect.ValueOf(space.Spec.ServiceAccounts) + if !serviceAccounts.IsZero() { + r.Log.Info("Reconciling ServiceAccounts for space") + err = r.reconcileServiceAccounts(ctx, space) + + if err != nil { + r.ProcessFailedCondition(ctx, space, constants.SpaceConditionFailed, metav1.ConditionFalse, constants.SpaceFailedReason, constants.SpaceSyncFailMessage) + r.setMetrics(space, nauticusiov1alpha1.ConditionType(constants.SpaceConditionFailed)) + + return ctrl.Result{}, err + } + } + + r.ProcessReadyCondition(ctx, space, constants.SpaceConditionReady, metav1.ConditionTrue, constants.SpaceSyncSuccessReason, constants.SpaceSyncSuccessMessage) + r.setMetrics(space, nauticusiov1alpha1.ConditionType(constants.SpaceConditionReady)) + + return ctrl.Result{ + RequeueAfter: constants.RequeueAfter, + }, nil +} + +func (r *Reconciler) reconcileSpaceFromTemplate(ctx context.Context, space *nauticusiov1alpha1.Space) (result reconcile.Result, err error) { + // Fetch data from the SpaceTemplate + spaceTpl, err := r.FetchSpaceTemplate(ctx, space.Spec.TemplateRef.Name) + if err != nil { + r.Log.Info("SpaceTemplate:", "SpaceTemplate does not exist", space.Spec.TemplateRef.Name) + + return ctrl.Result{}, err + } + + // Merge and override space.ResourceQuota and spacetemplate.ResourceQuota + rs, err := MergeResourceQuotas(space, spaceTpl) + if err == nil { + space.Spec.ResourceQuota = *rs + } + // Merge and override space.AdditionalRoleBindings and spacetemplate.AdditionalRoleBindings + rb, err := MergeRoleBindings(space, spaceTpl) + if err == nil { + space.Spec.AdditionalRoleBindings = rb + } + + if reflect.ValueOf(space.Spec.NetworkPolicies).IsZero() { + space.Spec.NetworkPolicies = spaceTpl.Spec.NetworkPolicies + } + + if reflect.ValueOf(space.Spec.LimitRanges).IsZero() { + space.Spec.LimitRanges = spaceTpl.Spec.LimitRanges + } + // Create or update the Space in the cluster + r.Log.Info("Reconciling Space from", "SpaceTemplate", spaceTpl.Name) + + return r.reconcileSpace(ctx, space) +} + +func (r *Reconciler) reconcileDelete(ctx context.Context, space *nauticusiov1alpha1.Space) (result reconcile.Result, err error) { + // The annotation is set, so skip namespace deletion + // Just remove the finalizer from the Space + if space.HasIgnoreUnderlyingDeletionAnnotation() { + if controllerutil.ContainsFinalizer(space, constants.NauticusSpaceFinalizer) { + controllerutil.RemoveFinalizer(space, constants.NauticusSpaceFinalizer) + + if err = r.Update(ctx, space); err != nil { + return ctrl.Result{}, err + } + } + + return ctrl.Result{}, err + } + // If the annotation is not set, delete all created resources + if controllerutil.ContainsFinalizer(space, constants.NauticusSpaceFinalizer) { + if err = r.deleteNetworkPolicies(ctx, space); client.IgnoreNotFound(err) != nil { + return ctrl.Result{}, err + } + + if err = r.deleteLimitRanges(ctx, space); client.IgnoreNotFound(err) != nil { + return ctrl.Result{}, err + } + + if err = r.deleteOwners(ctx, space); client.IgnoreNotFound(err) != nil { + return ctrl.Result{}, err + } + + if err = r.deleteAdditionalRoleBindings(ctx, space); client.IgnoreNotFound(err) != nil { + return ctrl.Result{}, err + } + + if err = r.deleteResourceQuota(ctx, space); client.IgnoreNotFound(err) != nil { + return ctrl.Result{}, err + } + + if err = r.deleteServiceAccounts(ctx, space); client.IgnoreNotFound(err) != nil { + return ctrl.Result{}, err + } + + if err = r.deleteNamespace(ctx, space); client.IgnoreNotFound(err) != nil { + return ctrl.Result{}, err + } + + // remove our finalizer from the list and update it. + controllerutil.RemoveFinalizer(space, constants.NauticusSpaceFinalizer) + + if err = r.Update(ctx, space); err != nil { + return ctrl.Result{}, err + } + } + // Stop reconciliation as the item is being deleted + return ctrl.Result{}, err +} diff --git a/controllers/resource_quota.go b/controllers/space/resource_quota.go similarity index 65% rename from controllers/resource_quota.go rename to controllers/space/resource_quota.go index b5ca7ca..c174fcd 100644 --- a/controllers/resource_quota.go +++ b/controllers/space/resource_quota.go @@ -1,7 +1,7 @@ // Copyright 2022-2023 Edixos // SPDX-License-Identifier: Apache-2.0 -package controllers +package space import ( "context" @@ -13,7 +13,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" ) -func (s *SpaceReconciler) reconcileResourceQuota(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { +func (r *Reconciler) reconcileResourceQuota(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { resourceQuota := &corev1.ResourceQuota{ ObjectMeta: metav1.ObjectMeta{ Name: space.Name, @@ -21,12 +21,12 @@ func (s *SpaceReconciler) reconcileResourceQuota(ctx context.Context, space *nau }, Spec: space.Spec.ResourceQuota, } - err = s.syncResourceQuotas(ctx, resourceQuota, space) + err = r.syncResourceQuotas(ctx, resourceQuota, space) return err } -func (s *SpaceReconciler) syncResourceQuotas(ctx context.Context, resourceQuota *corev1.ResourceQuota, space *nauticusiov1alpha1.Space) (err error) { +func (r *Reconciler) syncResourceQuotas(ctx context.Context, resourceQuota *corev1.ResourceQuota, space *nauticusiov1alpha1.Space) (err error) { var ( res controllerutil.OperationResult spaceLabel, resourceQuotaLabel string @@ -40,7 +40,7 @@ func (s *SpaceReconciler) syncResourceQuotas(ctx context.Context, resourceQuota return } - res, err = controllerutil.CreateOrUpdate(ctx, s.Client, resourceQuota, func() error { + res, err = controllerutil.CreateOrUpdate(ctx, r.Client, resourceQuota, func() error { resourceQuota.SetLabels(map[string]string{ spaceLabel: space.Name, resourceQuotaLabel: resourceQuota.Name, @@ -49,13 +49,13 @@ func (s *SpaceReconciler) syncResourceQuotas(ctx context.Context, resourceQuota return nil }) - s.Log.Info("ResourceQuota sync result: "+string(res), "name", resourceQuota.Name) - s.emitEvent(space, space.Name, res, "Ensuring ResourceQuota creation/Update", err) + r.Log.Info("ResourceQuota sync result: "+string(res), "name", resourceQuota.Name) + r.EmitEvent(space, space.Name, res, "Ensuring ResourceQuota creation/Update", err) return err } -func (s *SpaceReconciler) deleteResourceQuota(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { +func (r *Reconciler) deleteResourceQuota(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { resourceQuota := &corev1.ResourceQuota{ ObjectMeta: metav1.ObjectMeta{ Name: space.Name, @@ -63,7 +63,7 @@ func (s *SpaceReconciler) deleteResourceQuota(ctx context.Context, space *nautic }, Spec: space.Spec.ResourceQuota, } - err = s.deleteObject(ctx, resourceQuota) + err = r.DeleteObject(ctx, resourceQuota) return err } diff --git a/controllers/service_account.go b/controllers/space/service_account.go similarity index 67% rename from controllers/service_account.go rename to controllers/space/service_account.go index 2fc0599..f8c8be2 100644 --- a/controllers/service_account.go +++ b/controllers/space/service_account.go @@ -1,7 +1,7 @@ // Copyright 2022-2023 Edixos // SPDX-License-Identifier: Apache-2.0 -package controllers +package space import ( "context" @@ -13,13 +13,13 @@ import ( "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" ) -func (s *SpaceReconciler) reconcileServiceAccounts(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { +func (r *Reconciler) reconcileServiceAccounts(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { for _, serviceAccount := range space.Spec.ServiceAccounts.Items { sa := newServiceAccount(serviceAccount.Name, space.Status.NamespaceName, serviceAccount.Annotations) - err = s.syncServiceAccount(ctx, sa, space, serviceAccount.Annotations) + err = r.syncServiceAccount(ctx, sa, space, serviceAccount.Annotations) if err != nil { - s.Log.Error(err, "Cannot Synchronize Service Account") + r.Log.Error(err, "Cannot Synchronize Service Account") return err } @@ -28,7 +28,7 @@ func (s *SpaceReconciler) reconcileServiceAccounts(ctx context.Context, space *n return nil } -func (s *SpaceReconciler) syncServiceAccount(ctx context.Context, serviceAccount *corev1.ServiceAccount, space *nauticusiov1alpha1.Space, annotations nauticusiov1alpha1.Annotations) (err error) { +func (r *Reconciler) syncServiceAccount(ctx context.Context, serviceAccount *corev1.ServiceAccount, space *nauticusiov1alpha1.Space, annotations nauticusiov1alpha1.Annotations) (err error) { var ( res controllerutil.OperationResult spaceLabel, serviceAccountLabel string @@ -42,7 +42,7 @@ func (s *SpaceReconciler) syncServiceAccount(ctx context.Context, serviceAccount return } - res, err = controllerutil.CreateOrUpdate(ctx, s.Client, serviceAccount, func() (err error) { + res, err = controllerutil.CreateOrUpdate(ctx, r.Client, serviceAccount, func() (err error) { serviceAccount.SetLabels(map[string]string{ spaceLabel: space.Name, serviceAccountLabel: serviceAccount.Name, @@ -51,8 +51,8 @@ func (s *SpaceReconciler) syncServiceAccount(ctx context.Context, serviceAccount return nil }) - s.Log.Info("ServiceAccount sync result: "+string(res), "name", serviceAccount.Name, "namespace", space.Status.NamespaceName) - s.emitEvent(space, space.Name, res, "Ensuring ServiceAccount creation/Update", err) + r.Log.Info("ServiceAccount sync result: "+string(res), "name", serviceAccount.Name, "namespace", space.Status.NamespaceName) + r.EmitEvent(space, space.Name, res, "Ensuring ServiceAccount creation/Update", err) return nil } @@ -67,10 +67,10 @@ func newServiceAccount(name, namespace string, annotations nauticusiov1alpha1.An } } -func (s *SpaceReconciler) deleteServiceAccounts(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { +func (r *Reconciler) deleteServiceAccounts(ctx context.Context, space *nauticusiov1alpha1.Space) (err error) { for _, serviceAccount := range space.Spec.ServiceAccounts.Items { sa := newServiceAccount(serviceAccount.Name, space.Status.NamespaceName, serviceAccount.Annotations) - if err = s.deleteObject(ctx, sa); err != nil { + if err = r.DeleteObject(ctx, sa); err != nil { return err } } diff --git a/controllers/space/utlis.go b/controllers/space/utlis.go new file mode 100644 index 0000000..ac5807d --- /dev/null +++ b/controllers/space/utlis.go @@ -0,0 +1,92 @@ +// Copyright 2022-2023 Edixos +// SPDX-License-Identifier: Apache-2.0 +package space + +import ( + "context" + "errors" + "reflect" + + nauticusiov1alpha1 "github.com/edixos/nauticus/api/v1alpha1" + corev1 "k8s.io/api/core/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" + + "sigs.k8s.io/controller-runtime/pkg/client" +) + +func (r *Reconciler) FetchSpaceTemplate(ctx context.Context, name string) (*nauticusiov1alpha1.SpaceTemplate, error) { + spaceTemplate := &nauticusiov1alpha1.SpaceTemplate{} + + err := r.Get(ctx, client.ObjectKey{ + Name: name, + }, spaceTemplate) + if err != nil { + if apierrors.IsNotFound(err) { + // SpaceTemplate not found, return + r.Log.Info("SpaceTemplate not found") + } + + return nil, err + } + + return spaceTemplate, nil +} + +func MergeResourceQuotas(space *nauticusiov1alpha1.Space, spacetpl *nauticusiov1alpha1.SpaceTemplate) (*corev1.ResourceQuotaSpec, error) { + resourceQuotas := &corev1.ResourceQuotaSpec{} + resourceQuotas.Hard = make(corev1.ResourceList) + // Check if resourceQuota is provided in the Space and spaceTemplate + switch { + case !reflect.ValueOf(space.Spec.ResourceQuota).IsZero() && !reflect.ValueOf(spacetpl.Spec.ResourceQuota).IsZero(): + overrideResourceQuotas(resourceQuotas, space.Spec.ResourceQuota.Hard, spacetpl.Spec.ResourceQuota.Hard, corev1.ResourceLimitsCPU) + overrideResourceQuotas(resourceQuotas, space.Spec.ResourceQuota.Hard, spacetpl.Spec.ResourceQuota.Hard, corev1.ResourceLimitsMemory) + overrideResourceQuotas(resourceQuotas, space.Spec.ResourceQuota.Hard, spacetpl.Spec.ResourceQuota.Hard, corev1.ResourceRequestsCPU) + overrideResourceQuotas(resourceQuotas, space.Spec.ResourceQuota.Hard, spacetpl.Spec.ResourceQuota.Hard, corev1.ResourceRequestsMemory) + case reflect.ValueOf(space.Spec.ResourceQuota).IsZero() && !reflect.ValueOf(spacetpl.Spec.ResourceQuota).IsZero(): + resourceQuotas.Hard = spacetpl.Spec.ResourceQuota.Hard + default: + err := errors.New("merge not required both space and spacetpl resource quotas are empty") + + return nil, err + } + + return resourceQuotas, nil +} + +func MergeRoleBindings(space *nauticusiov1alpha1.Space, spaceTemplate *nauticusiov1alpha1.SpaceTemplate) ([]nauticusiov1alpha1.AdditionalRoleBinding, error) { + mergedRoleBindings := append([]nauticusiov1alpha1.AdditionalRoleBinding{}, space.Spec.AdditionalRoleBindings...) + + for _, roleBinding := range spaceTemplate.Spec.AdditionalRoleBindings { + // Check if the role binding already exists in mergedRoleBindings + if !cmpRoleBinding(mergedRoleBindings, roleBinding) { + mergedRoleBindings = append(mergedRoleBindings, roleBinding) + } + } + + if len(mergedRoleBindings) > 0 { + return mergedRoleBindings, nil + } + + return nil, errors.New("no additional roles bindings merged from the template") +} + +func overrideResourceQuotas(resourceQuotas *corev1.ResourceQuotaSpec, spaceHard, templateHard corev1.ResourceList, resource corev1.ResourceName) { + if spaceValue, exists := spaceHard[resource]; exists { + resourceQuotas.Hard[resource] = spaceValue + } else { + resourceQuotas.Hard[resource] = templateHard[resource] + } +} + +func cmpRoleBinding(roleBindings []nauticusiov1alpha1.AdditionalRoleBinding, roleBinding nauticusiov1alpha1.AdditionalRoleBinding) bool { + for _, rb := range roleBindings { + if rb.RoleRef.Name == roleBinding.RoleRef.Name && rb.RoleRef.Kind == roleBinding.RoleRef.Kind { + // Check if subjects are equal + if reflect.DeepEqual(rb.Subjects, roleBinding.Subjects) { + return true + } + } + } + + return false +} diff --git a/controllers/space/utlis_test.go b/controllers/space/utlis_test.go new file mode 100644 index 0000000..af98887 --- /dev/null +++ b/controllers/space/utlis_test.go @@ -0,0 +1,348 @@ +// Copyright 2022-2023 Edixos +// SPDX-License-Identifier: Apache-2.0 +package space + +import ( + "errors" + "reflect" + "testing" + + nauticusiov1alpha1 "github.com/edixos/nauticus/api/v1alpha1" + corev1 "k8s.io/api/core/v1" + v1 "k8s.io/api/rbac/v1" + "k8s.io/apimachinery/pkg/api/resource" +) + +func TestMergeResourceQuotas(t *testing.T) { + testCases := []struct { + name string + space *nauticusiov1alpha1.Space + spaceTemplate *nauticusiov1alpha1.SpaceTemplate + expected *corev1.ResourceQuotaSpec + expectedErr error + }{ + { + name: "Both space and spaceTemplate ResourceQuotas provided", + space: &nauticusiov1alpha1.Space{ + Spec: nauticusiov1alpha1.SpaceSpec{ + ResourceQuota: corev1.ResourceQuotaSpec{ + Hard: corev1.ResourceList{ + corev1.ResourceLimitsCPU: resource.MustParse("8"), + corev1.ResourceLimitsMemory: resource.MustParse("16Gi"), + corev1.ResourceRequestsCPU: resource.MustParse("4"), + corev1.ResourceRequestsMemory: resource.MustParse("8Gi"), + }, + }, + }, + }, + spaceTemplate: &nauticusiov1alpha1.SpaceTemplate{ + Spec: nauticusiov1alpha1.SpaceTemplateSpec{ + ResourceQuota: corev1.ResourceQuotaSpec{ + Hard: corev1.ResourceList{ + corev1.ResourceLimitsCPU: resource.MustParse("2"), + corev1.ResourceLimitsMemory: resource.MustParse("2Gi"), + corev1.ResourceRequestsCPU: resource.MustParse("1"), + corev1.ResourceRequestsMemory: resource.MustParse("1Gi"), + }, + }, + }, + }, + expected: &corev1.ResourceQuotaSpec{ + Hard: corev1.ResourceList{ + corev1.ResourceLimitsCPU: resource.MustParse("8"), + corev1.ResourceLimitsMemory: resource.MustParse("16Gi"), + corev1.ResourceRequestsCPU: resource.MustParse("4"), + corev1.ResourceRequestsMemory: resource.MustParse("8Gi"), + }, + }, + expectedErr: nil, + }, + { + name: "Both space and spaceTemplate ResourceQuotas provided (CPU)", + space: &nauticusiov1alpha1.Space{ + Spec: nauticusiov1alpha1.SpaceSpec{ + ResourceQuota: corev1.ResourceQuotaSpec{ + Hard: corev1.ResourceList{ + corev1.ResourceLimitsCPU: resource.MustParse("8"), + corev1.ResourceRequestsCPU: resource.MustParse("4"), + corev1.ResourceLimitsMemory: resource.MustParse("1Gi"), + corev1.ResourceRequestsMemory: resource.MustParse("500Mi"), + }, + }, + }, + }, + spaceTemplate: &nauticusiov1alpha1.SpaceTemplate{ + Spec: nauticusiov1alpha1.SpaceTemplateSpec{ + ResourceQuota: corev1.ResourceQuotaSpec{ + Hard: corev1.ResourceList{ + corev1.ResourceLimitsCPU: resource.MustParse("2"), + corev1.ResourceRequestsCPU: resource.MustParse("1"), + corev1.ResourceLimitsMemory: resource.MustParse("1Gi"), + corev1.ResourceRequestsMemory: resource.MustParse("500Mi"), + }, + }, + }, + }, + expected: &corev1.ResourceQuotaSpec{ + Hard: corev1.ResourceList{ + corev1.ResourceLimitsCPU: resource.MustParse("8"), + corev1.ResourceRequestsCPU: resource.MustParse("4"), + corev1.ResourceLimitsMemory: resource.MustParse("1Gi"), + corev1.ResourceRequestsMemory: resource.MustParse("500Mi"), + }, + }, + expectedErr: nil, + }, + { + name: "Only spaceTemplate ResourceQuotas (limits) provided", + space: &nauticusiov1alpha1.Space{}, + spaceTemplate: &nauticusiov1alpha1.SpaceTemplate{ + Spec: nauticusiov1alpha1.SpaceTemplateSpec{ + ResourceQuota: corev1.ResourceQuotaSpec{ + Hard: corev1.ResourceList{ + corev1.ResourceLimitsCPU: resource.MustParse("3"), + corev1.ResourceLimitsMemory: resource.MustParse("3Gi"), + }, + }, + }, + }, + expected: &corev1.ResourceQuotaSpec{ + Hard: corev1.ResourceList{ + corev1.ResourceLimitsCPU: resource.MustParse("3"), + corev1.ResourceLimitsMemory: resource.MustParse("3Gi"), + }, + }, + expectedErr: nil, + }, + { + name: "Both space and spaceTemplate ResourceQuotas are empty", + space: &nauticusiov1alpha1.Space{}, + spaceTemplate: &nauticusiov1alpha1.SpaceTemplate{}, + expected: nil, + expectedErr: errors.New("merge not required both space and spacetpl resource quotas are empty"), + }, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result, err := MergeResourceQuotas(tc.space, tc.spaceTemplate) + + if !reflect.DeepEqual(tc.expected, result) { + t.Errorf("Expected: %v, Got: %v", tc.expected, result) + } + if !reflect.DeepEqual(tc.expectedErr, err) { + t.Errorf("Expected error: %v, Got error: %v", tc.expectedErr, err) + } + }) + } +} + +func TestMergeRoleBindings(t *testing.T) { + testCases := []struct { + name string + space *nauticusiov1alpha1.Space + spaceTemplate *nauticusiov1alpha1.SpaceTemplate + expected []nauticusiov1alpha1.AdditionalRoleBinding + expectedErr error + }{ + { + name: "Space has role bindings, SpaceTemplate has role bindings", + space: &nauticusiov1alpha1.Space{ + Spec: nauticusiov1alpha1.SpaceSpec{ + AdditionalRoleBindings: []nauticusiov1alpha1.AdditionalRoleBinding{ + { + RoleRef: v1.RoleRef{ + APIGroup: "rbac.authorization.k8s.io", + Kind: "ClusterRole", + Name: "editor", + }, + Subjects: []v1.Subject{ + { + Name: "bob", + Kind: "User", + }, + { + Name: "dev2", + Kind: "Group", + }, + }, + }, + }, + }, + }, + spaceTemplate: &nauticusiov1alpha1.SpaceTemplate{ + Spec: nauticusiov1alpha1.SpaceTemplateSpec{ + AdditionalRoleBindings: []nauticusiov1alpha1.AdditionalRoleBinding{ + { + RoleRef: v1.RoleRef{ + APIGroup: "rbac.authorization.k8s.io", + Kind: "ClusterRole", + Name: "viewer", + }, + Subjects: []v1.Subject{ + { + Name: "alice", + Kind: "User", + }, + { + Name: "dev", + Kind: "Group", + }, + }, + }, + }, + }, + }, + expected: []nauticusiov1alpha1.AdditionalRoleBinding{ + { + RoleRef: v1.RoleRef{ + APIGroup: "rbac.authorization.k8s.io", + Kind: "ClusterRole", + Name: "editor", + }, + Subjects: []v1.Subject{ + { + Name: "bob", + Kind: "User", + }, + { + Name: "dev2", + Kind: "Group", + }, + }, + }, + { + RoleRef: v1.RoleRef{ + APIGroup: "rbac.authorization.k8s.io", + Kind: "ClusterRole", + Name: "viewer", + }, + Subjects: []v1.Subject{ + { + Name: "alice", + Kind: "User", + }, + { + Name: "dev", + Kind: "Group", + }, + }, + }, + }, + expectedErr: nil, + }, + { + name: "Space has no role bindings, SpaceTemplate has role bindings", + space: &nauticusiov1alpha1.Space{}, + spaceTemplate: &nauticusiov1alpha1.SpaceTemplate{ + Spec: nauticusiov1alpha1.SpaceTemplateSpec{ + AdditionalRoleBindings: []nauticusiov1alpha1.AdditionalRoleBinding{ + { + RoleRef: v1.RoleRef{ + APIGroup: "rbac.authorization.k8s.io", + Kind: "ClusterRole", + Name: "viewer", + }, + Subjects: []v1.Subject{ + { + Name: "alice", + Kind: "User", + }, + { + Name: "dev", + Kind: "Group", + }, + }, + }, + }, + }, + }, + expected: []nauticusiov1alpha1.AdditionalRoleBinding{ + { + RoleRef: v1.RoleRef{ + APIGroup: "rbac.authorization.k8s.io", + Kind: "ClusterRole", + Name: "viewer", + }, + Subjects: []v1.Subject{ + { + Name: "alice", + Kind: "User", + }, + { + Name: "dev", + Kind: "Group", + }, + }, + }, + }, + expectedErr: nil, + }, + { + name: "Space has role bindings, SpaceTemplate has no role bindings", + space: &nauticusiov1alpha1.Space{ + Spec: nauticusiov1alpha1.SpaceSpec{ + AdditionalRoleBindings: []nauticusiov1alpha1.AdditionalRoleBinding{ + { + RoleRef: v1.RoleRef{ + APIGroup: "rbac.authorization.k8s.io", + Kind: "ClusterRole", + Name: "viewer", + }, + Subjects: []v1.Subject{ + { + Name: "alice", + Kind: "User", + }, + { + Name: "dev", + Kind: "Group", + }, + }, + }, + }, + }, + }, + spaceTemplate: &nauticusiov1alpha1.SpaceTemplate{}, + expected: []nauticusiov1alpha1.AdditionalRoleBinding{ + { + RoleRef: v1.RoleRef{ + APIGroup: "rbac.authorization.k8s.io", + Kind: "ClusterRole", + Name: "viewer", + }, + Subjects: []v1.Subject{ + { + Name: "alice", + Kind: "User", + }, + { + Name: "dev", + Kind: "Group", + }, + }, + }, + }, + expectedErr: nil, + }, + { + name: "Both Space and SpaceTemplate have no role bindings", + space: &nauticusiov1alpha1.Space{}, + spaceTemplate: &nauticusiov1alpha1.SpaceTemplate{}, + expected: nil, + expectedErr: errors.New("no additional roles bindings merged from the template"), + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result, err := MergeRoleBindings(tc.space, tc.spaceTemplate) + + if !reflect.DeepEqual(tc.expected, result) { + t.Errorf("Expected: %v, Got: %v", tc.expected, result) + } + if !reflect.DeepEqual(tc.expectedErr, err) { + t.Errorf("Expected error: %v, Got error: %v", tc.expectedErr, err) + } + }) + } +} diff --git a/controllers/spacetemplate/controller.go b/controllers/spacetemplate/controller.go new file mode 100644 index 0000000..582dbf0 --- /dev/null +++ b/controllers/spacetemplate/controller.go @@ -0,0 +1,62 @@ +// Copyright 2022-2023 Edixos +// SPDX-License-Identifier: Apache-2.0 + +package spacetemplate + +import ( + "context" + + nauticusiov1alpha1 "github.com/edixos/nauticus/api/v1alpha1" + "github.com/edixos/nauticus/controllers/shared" + apierrors "k8s.io/apimachinery/pkg/api/errors" + ctrl "sigs.k8s.io/controller-runtime" +) + +// Reconciler reconciles a SpaceTemplate object. +type Reconciler struct { + shared.Reconciler +} + +//+kubebuilder:rbac:groups=nauticus.io,resources=spacetemplates,verbs=get;list;watch;create;update;patch;delete +//+kubebuilder:rbac:groups=nauticus.io,resources=spacetemplates/status,verbs=get;update;patch +//+kubebuilder:rbac:groups=nauticus.io,resources=spacetemplates/finalizers,verbs=update + +// Reconcile is part of the main kubernetes reconciliation loop which aims to +// move the current state of the cluster closer to the desired state. +// the SpaceTemplate object against the actual cluster state, and then +// perform operations to make the cluster state reflect the state specified by +// the user. +// +// For more details, check Reconcile and its Result here: +// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.14.4/pkg/reconcile +func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + log := r.Log.WithValues("spacetemplate", req.NamespacedName) + + // fetch the spaceTemplate + spaceTpl := &nauticusiov1alpha1.SpaceTemplate{} + + err := r.Get(ctx, req.NamespacedName, spaceTpl) + if err != nil { + if apierrors.IsNotFound(err) { + // Space not found, return + log.Info("SpaceTemplate not found.") + + return ctrl.Result{}, nil + } + // Error reading the object - requeue the request. + return ctrl.Result{}, err + } + + if !spaceTpl.ObjectMeta.DeletionTimestamp.IsZero() { + return r.reconcileDeleteSpaceTemplate(ctx, spaceTpl) + } + + return r.reconcileSpaceTemplate(ctx, spaceTpl) +} + +// SetupWithManager sets up the controller with the Manager. +func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + For(&nauticusiov1alpha1.SpaceTemplate{}). + Complete(r) +} diff --git a/controllers/spacetemplate/metrics.go b/controllers/spacetemplate/metrics.go new file mode 100644 index 0000000..28391e6 --- /dev/null +++ b/controllers/spacetemplate/metrics.go @@ -0,0 +1,26 @@ +// Copyright 2022-2023 Edixos +// SPDX-License-Identifier: Apache-2.0 +package spacetemplate + +import ( + "github.com/edixos/nauticus/api/v1alpha1" + "github.com/edixos/nauticus/pkg/controller/constants" + "github.com/edixos/nauticus/pkg/metrics" +) + +func (r *Reconciler) setMetrics(spacetpl *v1alpha1.SpaceTemplate, conditionType v1alpha1.ConditionType) { + switch conditionType { + case v1alpha1.ConditionType(constants.SpaceTplConditionReady): + metrics.ReadySpaceTemplates.WithLabelValues(spacetpl.Name).Set(0) + metrics.InProgresSpaceTemplates.WithLabelValues(spacetpl.Name).Set(0) + metrics.FailedSpaceTemplates.WithLabelValues(spacetpl.Name).Set(0) + case v1alpha1.ConditionType(constants.SpaceTplConditionCreating): + metrics.ReadySpaceTemplates.WithLabelValues(spacetpl.Name).Set(0) + metrics.InProgresSpaceTemplates.WithLabelValues(spacetpl.Name).Set(0) + metrics.FailedSpaceTemplates.WithLabelValues(spacetpl.Name).Set(0) + case v1alpha1.ConditionType(constants.SpaceTplConditionFailed): + metrics.ReadySpaceTemplates.WithLabelValues(spacetpl.Name).Set(0) + metrics.InProgresSpaceTemplates.WithLabelValues(spacetpl.Name).Set(0) + metrics.FailedSpaceTemplates.WithLabelValues(spacetpl.Name).Set(0) + } +} diff --git a/controllers/spacetemplate/reconciler.go b/controllers/spacetemplate/reconciler.go new file mode 100644 index 0000000..ccac0e0 --- /dev/null +++ b/controllers/spacetemplate/reconciler.go @@ -0,0 +1,57 @@ +// Copyright 2022-2023 Edixos +// SPDX-License-Identifier: Apache-2.0 + +package spacetemplate + +import ( + "context" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + nauticusiov1alpha1 "github.com/edixos/nauticus/api/v1alpha1" + "github.com/edixos/nauticus/pkg/controller/constants" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + "sigs.k8s.io/controller-runtime/pkg/reconcile" +) + +func (r *Reconciler) reconcileSpaceTemplate(ctx context.Context, spaceTpl *nauticusiov1alpha1.SpaceTemplate) (result reconcile.Result, err error) { + if !controllerutil.ContainsFinalizer(spaceTpl, constants.NauticusSpaceFinalizer) { + controllerutil.AddFinalizer(spaceTpl, constants.NauticusSpaceFinalizer) + + if err = r.Update(ctx, spaceTpl); err != nil { + r.Log.Info("Reconciling SpaceTemplate") + r.ProcessInProgressCondition(ctx, spaceTpl, constants.SpaceTplConditionCreating, metav1.ConditionUnknown, constants.SpaceTplCreatingReason, constants.SpaceTplCreatingMessage) + r.setMetrics(spaceTpl, nauticusiov1alpha1.ConditionType(constants.SpaceTplConditionCreating)) + r.EmitEvent(spaceTpl, spaceTpl.GetName(), controllerutil.OperationResultCreated, constants.SpaceTplCreatingMessage, nil) + + return ctrl.Result{}, err + } + + r.ProcessFailedCondition(ctx, spaceTpl, constants.SpaceTplConditionFailed, metav1.ConditionFalse, constants.SpaceTplFailedReason, constants.SpaceTplFailedMessage) + r.setMetrics(spaceTpl, nauticusiov1alpha1.ConditionType(constants.SpaceTplConditionFailed)) + + r.EmitEvent(spaceTpl, spaceTpl.GetName(), controllerutil.OperationResultUpdatedStatus, constants.SpaceTplFailedMessage, nil) + } + + r.ProcessFailedCondition(ctx, spaceTpl, constants.SpaceTplConditionReady, metav1.ConditionTrue, constants.SpaceTplSyncSuccessReason, constants.SpaceTplSyncSuccessMessage) + r.setMetrics(spaceTpl, nauticusiov1alpha1.ConditionType(constants.SpaceTplConditionReady)) + + r.EmitEvent(spaceTpl, spaceTpl.GetName(), controllerutil.OperationResultUpdatedStatus, constants.SpaceTplSyncSuccessMessage, nil) + + return ctrl.Result{ + RequeueAfter: constants.RequeueAfter, + }, nil +} + +func (r *Reconciler) reconcileDeleteSpaceTemplate(ctx context.Context, spaceTpl *nauticusiov1alpha1.SpaceTemplate) (result reconcile.Result, err error) { + if controllerutil.ContainsFinalizer(spaceTpl, constants.NauticusSpaceFinalizer) { + controllerutil.RemoveFinalizer(spaceTpl, constants.NauticusSpaceFinalizer) + + if err = r.Update(ctx, spaceTpl); err != nil { + return ctrl.Result{}, err + } + } + + return ctrl.Result{}, err +} diff --git a/controllers/utils.go b/controllers/utils.go deleted file mode 100644 index c13d1ab..0000000 --- a/controllers/utils.go +++ /dev/null @@ -1,139 +0,0 @@ -// Copyright 2022-2023 Edixos -// SPDX-License-Identifier: Apache-2.0 - -package controllers - -import ( - "context" - - "github.com/edixos/nauticus/api/v1alpha1" - "github.com/edixos/nauticus/pkg/metrics" - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/utils/clock" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" -) - -// Clock is defined as a package var so it can be stubbed out during tests. -var Clock clock.Clock = clock.RealClock{} - -func (s *SpaceReconciler) emitEvent(object runtime.Object, name string, res controllerutil.OperationResult, msg string, err error) { - eventType := corev1.EventTypeNormal - - if err != nil { - eventType = corev1.EventTypeWarning - res = "Error" - } - - s.Recorder.AnnotatedEventf(object, map[string]string{"OperationResult": string(res)}, eventType, name, msg) -} - -func (s *SpaceReconciler) setSpaceCondition(space *v1alpha1.Space, observedGeneration int64, conditionType string, status metav1.ConditionStatus, reason, message string) { - newCondition := metav1.Condition{ - Type: conditionType, - Status: status, - Reason: reason, - Message: message, - } - nowTime := metav1.NewTime(Clock.Now()) - newCondition.LastTransitionTime = nowTime - - // Set the condition generation - newCondition.ObservedGeneration = observedGeneration - - // Search through existing conditions - for idx, cond := range space.Status.Conditions { - // Skip unrelated conditions - if cond.Type != conditionType { - continue - } - - // If this update doesn't contain a state transition, we don't update - // the conditions LastTransitionTime to Now() - if cond.Status == status { - newCondition.LastTransitionTime = cond.LastTransitionTime - } else { - s.Log.WithName(space.GetName()).Info("Found status change for Space condition, setting lastTransitionTime to", space.GetName(), nowTime) - } - - // Overwrite the existing condition - space.Status.Conditions[idx] = newCondition - - return - } - - // If we've not found an existing condition of this type, we simply insert - // the new condition into the slice. - space.Status.Conditions = append(space.Status.Conditions, newCondition) - s.Log.WithName(space.GetName()).Info("Setting lastTransitionTime for Space condition ", space.GetObjectMeta().GetName(), nowTime.Time) -} - -func (s *SpaceReconciler) processFailedCondition(ctx context.Context, space *v1alpha1.Space) { - s.setSpaceCondition( - space, - space.GetGeneration(), - string(SpaceConditionFailed), - SpaceConditionStatusFalse, - string(SpaceFailedReason), - string(SpaceSyncFailMessage), - ) - s.updateStatus(ctx, space) -} - -func (s *SpaceReconciler) processReadyCondition(ctx context.Context, space *v1alpha1.Space) { - s.setSpaceCondition( - space, - space.GetGeneration(), - string(SpaceConditionReady), - SpaceConditionStatusTrue, - string(SpaceSyncSuccessReason), - string(SpaceSyncSuccessMessage), - ) - s.updateStatus(ctx, space) -} - -func (s *SpaceReconciler) processInProgressCondition(ctx context.Context, space *v1alpha1.Space) { - s.setSpaceCondition( - space, - space.GetGeneration(), - string(SpaceConditionCreating), - SpaceConditionStatusUnknown, - string(SpaceCreatingReason), - string(SpaceCreatingMessage), - ) - s.updateStatus(ctx, space) -} - -func (s *SpaceReconciler) updateStatus(ctx context.Context, space *v1alpha1.Space) { - err := s.Client.Status().Update(ctx, space) - if err != nil { - s.Log.Info("Failed to update Space status", "space", space.Name) - } -} - -func (s *SpaceReconciler) setMetrics(space *v1alpha1.Space, conditionType v1alpha1.ConditionType) { - switch conditionType { - case SpaceConditionCreating: - metrics.ReadySpaces.WithLabelValues(space.Name).Set(0) - metrics.InProgressSpaces.WithLabelValues(space.Name).Set(1) - metrics.FailedSpaces.WithLabelValues(space.Name).Set(0) - case SpaceConditionReady: - metrics.ReadySpaces.WithLabelValues(space.Name).Set(1) - metrics.InProgressSpaces.WithLabelValues(space.Name).Set(0) - metrics.FailedSpaces.WithLabelValues(space.Name).Set(0) - case SpaceConditionFailed: - metrics.ReadySpaces.WithLabelValues(space.Name).Set(0) - metrics.InProgressSpaces.WithLabelValues(space.Name).Set(0) - metrics.FailedSpaces.WithLabelValues(space.Name).Set(1) - } -} - -func (s *SpaceReconciler) deleteObject(ctx context.Context, object client.Object) (err error) { - if err = s.Client.Delete(ctx, object); client.IgnoreNotFound(err) != nil { - return err - } - - return nil -} diff --git a/docs/crds-apis.md b/docs/crds-apis.md index 6beaf99..4be7bbf 100644 --- a/docs/crds-apis.md +++ b/docs/crds-apis.md @@ -27,6 +27,8 @@ Resource Types: - [Space](#space) +- [SpaceTemplate](#spacetemplate) + @@ -140,6 +142,13 @@ SpaceSpec defines the desired state of Space. Specifies a list of service account to create within the Space. Optional
false + + templateRef + object + + Reference to a SpaceTemplate
+ + false @@ -1342,6 +1351,46 @@ Specifies a list of service account to create within the Space. Optional +### Space.spec.templateRef + + + +Reference to a SpaceTemplate + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
groupstring + Group is the API group of the SpaceTemplate, "nauticus.io/v1alpha1".
+
false
kindstring + Kind specifies the kind of the referenced resource, which should be "SpaceTemplate".
+
false
namestring + Name of the SpaceTemplate.
+
false
+ + ### Space.status @@ -1379,6 +1428,1293 @@ SpaceStatus defines the observed state of Space. +Condition contains details for one aspect of the current state of this API Resource. --- This struct is intended for direct use as an array at the field path .status.conditions. For example, + type FooStatus struct{ // Represents the observations of a foo's current state. // Known .status.conditions.type are: "Available", "Progressing", and "Degraded" // +patchMergeKey=type // +patchStrategy=merge // +listType=map // +listMapKey=type Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type" protobuf:"bytes,1,rep,name=conditions"` + // other fields } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
lastTransitionTimestring + lastTransitionTime is the last time the condition transitioned from one status to another. This should be when the underlying condition changed. If that is not known, then using the time when the API field changed is acceptable.
+
+ Format: date-time
+
true
messagestring + message is a human readable message indicating details about the transition. This may be an empty string.
+
true
reasonstring + reason contains a programmatic identifier indicating the reason for the condition's last transition. Producers of specific condition types may define expected values and meanings for this field, and whether the values are considered a guaranteed API. The value should be a CamelCase string. This field may not be empty.
+
true
statusenum + status of the condition, one of True, False, Unknown.
+
+ Enum: True, False, Unknown
+
true
typestring + type of condition in CamelCase or in foo.example.com/CamelCase. --- Many .condition.type values are consistent across resources like Available, but because arbitrary conditions can be useful (see .node.status.conditions), the ability to deconflict is important. The regex it matches is (dns1123SubdomainFmt/)?(qualifiedNameFmt)
+
true
observedGenerationinteger + observedGeneration represents the .metadata.generation that the condition was set based upon. For instance, if .metadata.generation is currently 12, but the .status.conditions[x].observedGeneration is 9, the condition is out of date with respect to the current state of the instance.
+
+ Format: int64
+ Minimum: 0
+
false
+ +## SpaceTemplate + + + + + + +SpaceTemplate is the Schema for the spacetemplates API. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
apiVersionstringnauticus.io/v1alpha1true
kindstringSpaceTemplatetrue
metadataobjectRefer to the Kubernetes API documentation for the fields of the `metadata` field.true
specobject + SpaceTemplateSpec defines the desired state of SpaceTemplate.
+
false
statusobject + SpaceTemplateStatus defines the observed state of SpaceTemplate.
+
false
+ + +### SpaceTemplate.spec + + + +SpaceTemplateSpec defines the desired state of SpaceTemplate. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
additionalRoleBindings[]object + Specifies additional RoleBindings assigned to the Space. Nauticus will ensure that the namespace in the Space always contain the RoleBinding for the given ClusterRole. Optional.
+
false
limitRangesobject + Specifies the resource min/max usage restrictions to the Space. Optional.
+
false
networkPoliciesobject + Specifies the NetworkPolicies assigned to the Tenant. The assigned NetworkPolicies are inherited by the namespace created in the Space. Optional.
+
false
resourceQuotaobject + Specifies a list of ResourceQuota resources assigned to the Space. The assigned values are inherited by the namespace created by the Space. Optional.
+
false
+ + +### SpaceTemplate.spec.additionalRoleBindings[index] + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
roleRefobject + RoleRef contains information that points to the role being used
+
false
subjects[]object +
+
false
+ + +### SpaceTemplate.spec.additionalRoleBindings[index].roleRef + + + +RoleRef contains information that points to the role being used + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
apiGroupstring + APIGroup is the group for the resource being referenced
+
true
kindstring + Kind is the type of resource being referenced
+
true
namestring + Name is the name of resource being referenced
+
true
+ + +### SpaceTemplate.spec.additionalRoleBindings[index].subjects[index] + + + +Subject contains a reference to the object or user identities a role binding applies to. This can either hold a direct API object reference, or a value for non-objects such as user and group names. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
kindstring + Kind of object being referenced. Values defined by this API group are "User", "Group", and "ServiceAccount". If the Authorizer does not recognized the kind value, the Authorizer should report an error.
+
true
namestring + Name of the object being referenced.
+
true
apiGroupstring + APIGroup holds the API group of the referenced subject. Defaults to "" for ServiceAccount subjects. Defaults to "rbac.authorization.k8s.io" for User and Group subjects.
+
false
namespacestring + Namespace of the referenced object. If the object kind is non-namespace, such as "User" or "Group", and this value is not empty the Authorizer should report an error.
+
false
+ + +### SpaceTemplate.spec.limitRanges + + + +Specifies the resource min/max usage restrictions to the Space. Optional. + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
items[]object +
+
false
+ + +### SpaceTemplate.spec.limitRanges.items[index] + + + +LimitRangeSpec defines a min/max usage limit for resources that match on kind. + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
limits[]object + Limits is the list of LimitRangeItem objects that are enforced.
+
true
+ + +### SpaceTemplate.spec.limitRanges.items[index].limits[index] + + + +LimitRangeItem defines a min/max usage limit for any resource that matches on kind. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
typestring + Type of resource that this limit applies to.
+
true
defaultmap[string]int or string + Default resource requirement limit value by resource name if resource limit is omitted.
+
false
defaultRequestmap[string]int or string + DefaultRequest is the default resource requirement request value by resource name if resource request is omitted.
+
false
maxmap[string]int or string + Max usage constraints on this kind by resource name.
+
false
maxLimitRequestRatiomap[string]int or string + MaxLimitRequestRatio if specified, the named resource must have a request and limit that are both non-zero where limit divided by request is less than or equal to the enumerated value; this represents the max burst for the named resource.
+
false
minmap[string]int or string + Min usage constraints on this kind by resource name.
+
false
+ + +### SpaceTemplate.spec.networkPolicies + + + +Specifies the NetworkPolicies assigned to the Tenant. The assigned NetworkPolicies are inherited by the namespace created in the Space. Optional. + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
enableDefaultStrictModeboolean +
+
false
items[]object +
+
false
+ + +### SpaceTemplate.spec.networkPolicies.items[index] + + + +NetworkPolicySpec provides the specification of a NetworkPolicy + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
podSelectorobject + Selects the pods to which this NetworkPolicy object applies. The array of ingress rules is applied to any pods selected by this field. Multiple network policies can select the same set of pods. In this case, the ingress rules for each are combined additively. This field is NOT optional and follows standard label selector semantics. An empty podSelector matches all pods in this namespace.
+
true
egress[]object + List of egress rules to be applied to the selected pods. Outgoing traffic is allowed if there are no NetworkPolicies selecting the pod (and cluster policy otherwise allows the traffic), OR if the traffic matches at least one egress rule across all of the NetworkPolicy objects whose podSelector matches the pod. If this field is empty then this NetworkPolicy limits all outgoing traffic (and serves solely to ensure that the pods it selects are isolated by default). This field is beta-level in 1.8
+
false
ingress[]object + List of ingress rules to be applied to the selected pods. Traffic is allowed to a pod if there are no NetworkPolicies selecting the pod (and cluster policy otherwise allows the traffic), OR if the traffic source is the pod's local node, OR if the traffic matches at least one ingress rule across all of the NetworkPolicy objects whose podSelector matches the pod. If this field is empty then this NetworkPolicy does not allow any traffic (and serves solely to ensure that the pods it selects are isolated by default)
+
false
policyTypes[]string + List of rule types that the NetworkPolicy relates to. Valid options are ["Ingress"], ["Egress"], or ["Ingress", "Egress"]. If this field is not specified, it will default based on the existence of Ingress or Egress rules; policies that contain an Egress section are assumed to affect Egress, and all policies (whether or not they contain an Ingress section) are assumed to affect Ingress. If you want to write an egress-only policy, you must explicitly specify policyTypes [ "Egress" ]. Likewise, if you want to write a policy that specifies that no egress is allowed, you must specify a policyTypes value that include "Egress" (since such a policy would not include an Egress section and would otherwise default to just [ "Ingress" ]). This field is beta-level in 1.8
+
false
+ + +### SpaceTemplate.spec.networkPolicies.items[index].podSelector + + + +Selects the pods to which this NetworkPolicy object applies. The array of ingress rules is applied to any pods selected by this field. Multiple network policies can select the same set of pods. In this case, the ingress rules for each are combined additively. This field is NOT optional and follows standard label selector semantics. An empty podSelector matches all pods in this namespace. + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
matchExpressions[]object + matchExpressions is a list of label selector requirements. The requirements are ANDed.
+
false
matchLabelsmap[string]string + matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is "key", the operator is "In", and the values array contains only "value". The requirements are ANDed.
+
false
+ + +### SpaceTemplate.spec.networkPolicies.items[index].podSelector.matchExpressions[index] + + + +A label selector requirement is a selector that contains values, a key, and an operator that relates the key and values. + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
keystring + key is the label key that the selector applies to.
+
true
operatorstring + operator represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist.
+
true
values[]string + values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch.
+
false
+ + +### SpaceTemplate.spec.networkPolicies.items[index].egress[index] + + + +NetworkPolicyEgressRule describes a particular set of traffic that is allowed out of pods matched by a NetworkPolicySpec's podSelector. The traffic must match both ports and to. This type is beta-level in 1.8 + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
ports[]object + List of destination ports for outgoing traffic. Each item in this list is combined using a logical OR. If this field is empty or missing, this rule matches all ports (traffic not restricted by port). If this field is present and contains at least one item, then this rule allows traffic only if the traffic matches at least one port in the list.
+
false
to[]object + List of destinations for outgoing traffic of pods selected for this rule. Items in this list are combined using a logical OR operation. If this field is empty or missing, this rule matches all destinations (traffic not restricted by destination). If this field is present and contains at least one item, this rule allows traffic only if the traffic matches at least one item in the to list.
+
false
+ + +### SpaceTemplate.spec.networkPolicies.items[index].egress[index].ports[index] + + + +NetworkPolicyPort describes a port to allow traffic on + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
endPortinteger + If set, indicates that the range of ports from port to endPort, inclusive, should be allowed by the policy. This field cannot be defined if the port field is not defined or if the port field is defined as a named (string) port. The endPort must be equal or greater than port.
+
+ Format: int32
+
false
portint or string + The port on the given protocol. This can either be a numerical or named port on a pod. If this field is not provided, this matches all port names and numbers. If present, only traffic on the specified protocol AND port will be matched.
+
false
protocolstring + The protocol (TCP, UDP, or SCTP) which traffic must match. If not specified, this field defaults to TCP.
+
+ Default: TCP
+
false
+ + +### SpaceTemplate.spec.networkPolicies.items[index].egress[index].to[index] + + + +NetworkPolicyPeer describes a peer to allow traffic to/from. Only certain combinations of fields are allowed + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
ipBlockobject + IPBlock defines policy on a particular IPBlock. If this field is set then neither of the other fields can be.
+
false
namespaceSelectorobject + Selects Namespaces using cluster-scoped labels. This field follows standard label selector semantics; if present but empty, it selects all namespaces. + If PodSelector is also set, then the NetworkPolicyPeer as a whole selects the Pods matching PodSelector in the Namespaces selected by NamespaceSelector. Otherwise it selects all Pods in the Namespaces selected by NamespaceSelector.
+
false
podSelectorobject + This is a label selector which selects Pods. This field follows standard label selector semantics; if present but empty, it selects all pods. + If NamespaceSelector is also set, then the NetworkPolicyPeer as a whole selects the Pods matching PodSelector in the Namespaces selected by NamespaceSelector. Otherwise it selects the Pods matching PodSelector in the policy's own Namespace.
+
false
+ + +### SpaceTemplate.spec.networkPolicies.items[index].egress[index].to[index].ipBlock + + + +IPBlock defines policy on a particular IPBlock. If this field is set then neither of the other fields can be. + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
cidrstring + CIDR is a string representing the IP Block Valid examples are "192.168.1.1/24" or "2001:db9::/64"
+
true
except[]string + Except is a slice of CIDRs that should not be included within an IP Block Valid examples are "192.168.1.1/24" or "2001:db9::/64" Except values will be rejected if they are outside the CIDR range
+
false
+ + +### SpaceTemplate.spec.networkPolicies.items[index].egress[index].to[index].namespaceSelector + + + +Selects Namespaces using cluster-scoped labels. This field follows standard label selector semantics; if present but empty, it selects all namespaces. + If PodSelector is also set, then the NetworkPolicyPeer as a whole selects the Pods matching PodSelector in the Namespaces selected by NamespaceSelector. Otherwise it selects all Pods in the Namespaces selected by NamespaceSelector. + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
matchExpressions[]object + matchExpressions is a list of label selector requirements. The requirements are ANDed.
+
false
matchLabelsmap[string]string + matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is "key", the operator is "In", and the values array contains only "value". The requirements are ANDed.
+
false
+ + +### SpaceTemplate.spec.networkPolicies.items[index].egress[index].to[index].namespaceSelector.matchExpressions[index] + + + +A label selector requirement is a selector that contains values, a key, and an operator that relates the key and values. + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
keystring + key is the label key that the selector applies to.
+
true
operatorstring + operator represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist.
+
true
values[]string + values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch.
+
false
+ + +### SpaceTemplate.spec.networkPolicies.items[index].egress[index].to[index].podSelector + + + +This is a label selector which selects Pods. This field follows standard label selector semantics; if present but empty, it selects all pods. + If NamespaceSelector is also set, then the NetworkPolicyPeer as a whole selects the Pods matching PodSelector in the Namespaces selected by NamespaceSelector. Otherwise it selects the Pods matching PodSelector in the policy's own Namespace. + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
matchExpressions[]object + matchExpressions is a list of label selector requirements. The requirements are ANDed.
+
false
matchLabelsmap[string]string + matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is "key", the operator is "In", and the values array contains only "value". The requirements are ANDed.
+
false
+ + +### SpaceTemplate.spec.networkPolicies.items[index].egress[index].to[index].podSelector.matchExpressions[index] + + + +A label selector requirement is a selector that contains values, a key, and an operator that relates the key and values. + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
keystring + key is the label key that the selector applies to.
+
true
operatorstring + operator represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist.
+
true
values[]string + values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch.
+
false
+ + +### SpaceTemplate.spec.networkPolicies.items[index].ingress[index] + + + +NetworkPolicyIngressRule describes a particular set of traffic that is allowed to the pods matched by a NetworkPolicySpec's podSelector. The traffic must match both ports and from. + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
from[]object + List of sources which should be able to access the pods selected for this rule. Items in this list are combined using a logical OR operation. If this field is empty or missing, this rule matches all sources (traffic not restricted by source). If this field is present and contains at least one item, this rule allows traffic only if the traffic matches at least one item in the from list.
+
false
ports[]object + List of ports which should be made accessible on the pods selected for this rule. Each item in this list is combined using a logical OR. If this field is empty or missing, this rule matches all ports (traffic not restricted by port). If this field is present and contains at least one item, then this rule allows traffic only if the traffic matches at least one port in the list.
+
false
+ + +### SpaceTemplate.spec.networkPolicies.items[index].ingress[index].from[index] + + + +NetworkPolicyPeer describes a peer to allow traffic to/from. Only certain combinations of fields are allowed + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
ipBlockobject + IPBlock defines policy on a particular IPBlock. If this field is set then neither of the other fields can be.
+
false
namespaceSelectorobject + Selects Namespaces using cluster-scoped labels. This field follows standard label selector semantics; if present but empty, it selects all namespaces. + If PodSelector is also set, then the NetworkPolicyPeer as a whole selects the Pods matching PodSelector in the Namespaces selected by NamespaceSelector. Otherwise it selects all Pods in the Namespaces selected by NamespaceSelector.
+
false
podSelectorobject + This is a label selector which selects Pods. This field follows standard label selector semantics; if present but empty, it selects all pods. + If NamespaceSelector is also set, then the NetworkPolicyPeer as a whole selects the Pods matching PodSelector in the Namespaces selected by NamespaceSelector. Otherwise it selects the Pods matching PodSelector in the policy's own Namespace.
+
false
+ + +### SpaceTemplate.spec.networkPolicies.items[index].ingress[index].from[index].ipBlock + + + +IPBlock defines policy on a particular IPBlock. If this field is set then neither of the other fields can be. + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
cidrstring + CIDR is a string representing the IP Block Valid examples are "192.168.1.1/24" or "2001:db9::/64"
+
true
except[]string + Except is a slice of CIDRs that should not be included within an IP Block Valid examples are "192.168.1.1/24" or "2001:db9::/64" Except values will be rejected if they are outside the CIDR range
+
false
+ + +### SpaceTemplate.spec.networkPolicies.items[index].ingress[index].from[index].namespaceSelector + + + +Selects Namespaces using cluster-scoped labels. This field follows standard label selector semantics; if present but empty, it selects all namespaces. + If PodSelector is also set, then the NetworkPolicyPeer as a whole selects the Pods matching PodSelector in the Namespaces selected by NamespaceSelector. Otherwise it selects all Pods in the Namespaces selected by NamespaceSelector. + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
matchExpressions[]object + matchExpressions is a list of label selector requirements. The requirements are ANDed.
+
false
matchLabelsmap[string]string + matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is "key", the operator is "In", and the values array contains only "value". The requirements are ANDed.
+
false
+ + +### SpaceTemplate.spec.networkPolicies.items[index].ingress[index].from[index].namespaceSelector.matchExpressions[index] + + + +A label selector requirement is a selector that contains values, a key, and an operator that relates the key and values. + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
keystring + key is the label key that the selector applies to.
+
true
operatorstring + operator represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist.
+
true
values[]string + values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch.
+
false
+ + +### SpaceTemplate.spec.networkPolicies.items[index].ingress[index].from[index].podSelector + + + +This is a label selector which selects Pods. This field follows standard label selector semantics; if present but empty, it selects all pods. + If NamespaceSelector is also set, then the NetworkPolicyPeer as a whole selects the Pods matching PodSelector in the Namespaces selected by NamespaceSelector. Otherwise it selects the Pods matching PodSelector in the policy's own Namespace. + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
matchExpressions[]object + matchExpressions is a list of label selector requirements. The requirements are ANDed.
+
false
matchLabelsmap[string]string + matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is "key", the operator is "In", and the values array contains only "value". The requirements are ANDed.
+
false
+ + +### SpaceTemplate.spec.networkPolicies.items[index].ingress[index].from[index].podSelector.matchExpressions[index] + + + +A label selector requirement is a selector that contains values, a key, and an operator that relates the key and values. + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
keystring + key is the label key that the selector applies to.
+
true
operatorstring + operator represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist.
+
true
values[]string + values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch.
+
false
+ + +### SpaceTemplate.spec.networkPolicies.items[index].ingress[index].ports[index] + + + +NetworkPolicyPort describes a port to allow traffic on + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
endPortinteger + If set, indicates that the range of ports from port to endPort, inclusive, should be allowed by the policy. This field cannot be defined if the port field is not defined or if the port field is defined as a named (string) port. The endPort must be equal or greater than port.
+
+ Format: int32
+
false
portint or string + The port on the given protocol. This can either be a numerical or named port on a pod. If this field is not provided, this matches all port names and numbers. If present, only traffic on the specified protocol AND port will be matched.
+
false
protocolstring + The protocol (TCP, UDP, or SCTP) which traffic must match. If not specified, this field defaults to TCP.
+
+ Default: TCP
+
false
+ + +### SpaceTemplate.spec.resourceQuota + + + +Specifies a list of ResourceQuota resources assigned to the Space. The assigned values are inherited by the namespace created by the Space. Optional. + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
hardmap[string]int or string + hard is the set of desired hard limits for each named resource. More info: https://kubernetes.io/docs/concepts/policy/resource-quotas/
+
false
scopeSelectorobject + scopeSelector is also a collection of filters like scopes that must match each object tracked by a quota but expressed using ScopeSelectorOperator in combination with possible values. For a resource to match, both scopes AND scopeSelector (if specified in spec), must be matched.
+
false
scopes[]string + A collection of filters that must match each object tracked by a quota. If not specified, the quota matches all objects.
+
false
+ + +### SpaceTemplate.spec.resourceQuota.scopeSelector + + + +scopeSelector is also a collection of filters like scopes that must match each object tracked by a quota but expressed using ScopeSelectorOperator in combination with possible values. For a resource to match, both scopes AND scopeSelector (if specified in spec), must be matched. + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
matchExpressions[]object + A list of scope selector requirements by scope of the resources.
+
false
+ + +### SpaceTemplate.spec.resourceQuota.scopeSelector.matchExpressions[index] + + + +A scoped-resource selector requirement is a selector that contains values, a scope name, and an operator that relates the scope name and values. + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
operatorstring + Represents a scope's relationship to a set of values. Valid operators are In, NotIn, Exists, DoesNotExist.
+
true
scopeNamestring + The name of the scope that the selector applies to.
+
true
values[]string + An array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch.
+
false
+ + +### SpaceTemplate.status + + + +SpaceTemplateStatus defines the observed state of SpaceTemplate. + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
conditions[]object + Conditions List of status conditions to indicate the status of Space
+
false
+ + +### SpaceTemplate.status.conditions[index] + + + Condition contains details for one aspect of the current state of this API Resource. --- This struct is intended for direct use as an array at the field path .status.conditions. For example, type FooStatus struct{ // Represents the observations of a foo's current state. // Known .status.conditions.type are: "Available", "Progressing", and "Degraded" // +patchMergeKey=type // +patchStrategy=merge // +listType=map // +listMapKey=type Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type" protobuf:"bytes,1,rep,name=conditions"` // other fields } diff --git a/docs/tutorials/space-templates-tutorial.md b/docs/tutorials/space-templates-tutorial.md new file mode 100644 index 0000000..16ca812 --- /dev/null +++ b/docs/tutorials/space-templates-tutorial.md @@ -0,0 +1,46 @@ +## Introduction + +The current process for creating Spaces with Nauticus, while straightforward, often involves repetitive configuration of the same parameters. This redundancy can hinder efficiency and result in time-consuming manual work. To address these challenges, the Space Templates feature has been developed. It allows administrators to create predefined templates that encompass common configurations, making Space creation more efficient and standardized. + +## Space Templates + +Space Templates are pre-configured templates that administrators can create to define common settings and configurations. These templates can include resource quotas, network policies, role bindings, and more. By referencing a Space Template during Space creation, users can take advantage of these predefined settings. + +## Usage + + +### SpaceTemplate Reference example +In this example, we will demonstrate how to create a SpaceTemplate in Nauticus that combines all the features so far: resource quota, network policy, Limit ranges and additional role bindings +```yaml title="config/samples/space_template_with_all.yaml" +{% include "../../config/samples/space_template_with_all.yaml" %} +``` + +To utilize Space Templates, users can reference a Space Template in their Space resource during creation. Any specifications provided in the Space resource will override the corresponding parameters in the Space Template. Here's an example of how this works: + +```yaml title="config/samples/space_with_template_ref.yaml" +{% include "../../config/samples/space_with_template_ref.yaml" %} +``` + +### Overrides +One of the key features of Space Templates is the ability to override specific configurations when referencing a template in a Space. Users can selectively modify parameters to match the requirements of their Space while still benefiting from the predefined template. + +By understanding the concept of overrides, administrators can take full advantage of Space Templates and achieve a balance between standardization and flexibility when configuring Spaces. +#### Example: Overriding ResourceQuotas +In this example, a Space named `space-tpl-ref-override` references a SpaceTemplate called `space-tpl-sample`. While leveraging predefined configurations from the template, it overrides the default resource quotas with custom values. + +* The `templateRef` references the `space-tpl-sample` SpaceTemplate. +* This demonstrates the flexibility of SpaceTemplates, allowing Spaces to maintain standard configurations while adjusting specific settings as needed. + + ```yaml title="config/samples/space_with_tpl_ref_overrides.yaml" + {% include "../../config/samples/space_with_tpl_ref_overrides.yaml" %} + ``` +#### Example: Merging Additional Role Bindings + +In this example, a Space named `space-tpl-ref-merge` references a Space Template called `space-tpl-sample`. It merges additional role bindings with predefined configurations from the template. This demonstrates the capability to combine and customize various settings while maintaining consistency. + +* The templateRef references the `space-tpl-sample` SpaceTemplate. +* The Space includes additional role bindings for both `viewer` and `editor` roles, with specific subjects. +* The merged role bindings enrich the Space's access control settings, ensuring flexibility and control. + ```yaml title="config/samples/space_with_tpl_merge.yaml" + {% include "../../config/samples/space_with_tpl_merge.yaml" %} + ``` \ No newline at end of file diff --git a/e2e/suite_test.go b/e2e/suite_test.go index 1c18541..6a07ba8 100644 --- a/e2e/suite_test.go +++ b/e2e/suite_test.go @@ -8,7 +8,8 @@ import ( "path/filepath" "testing" - "github.com/edixos/nauticus/controllers" + "github.com/edixos/nauticus/controllers/shared" + "github.com/edixos/nauticus/controllers/space" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "k8s.io/client-go/kubernetes/scheme" @@ -71,11 +72,13 @@ var _ = BeforeSuite(func() { }) Expect(err).ToNot(HaveOccurred()) - err = (&controllers.SpaceReconciler{ - Client: k8sManager.GetClient(), - Scheme: k8sManager.GetScheme(), - Log: ctrl.Log.WithName("controllers").WithName("Space"), - Recorder: k8sManager.GetEventRecorderFor("space-controller"), + err = (&space.Reconciler{ + Reconciler: shared.Reconciler{ + Client: k8sManager.GetClient(), + Scheme: k8sManager.GetScheme(), + Log: ctrl.Log.WithName("controllers").WithName("Space"), + Recorder: k8sManager.GetEventRecorderFor("space-controller"), + }, }).SetupWithManager(k8sManager) Expect(err).ToNot(HaveOccurred()) diff --git a/go.mod b/go.mod index a7db6f2..acb0fd1 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/go-logr/logr v1.2.3 github.com/onsi/ginkgo/v2 v2.1.4 github.com/onsi/gomega v1.19.0 + github.com/prometheus/client_golang v1.12.2 k8s.io/api v0.25.0 k8s.io/apimachinery v0.25.0 k8s.io/client-go v0.25.0 @@ -50,7 +51,6 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/pkg/errors v0.9.1 // indirect - github.com/prometheus/client_golang v1.12.2 // indirect github.com/prometheus/client_model v0.2.0 // indirect github.com/prometheus/common v0.32.1 // indirect github.com/prometheus/procfs v0.7.3 // indirect diff --git a/main.go b/main.go index f6e4096..4ed2abe 100644 --- a/main.go +++ b/main.go @@ -7,20 +7,24 @@ import ( "flag" "fmt" "os" - goRuntime "runtime" + "github.com/edixos/nauticus/controllers/shared" + "github.com/edixos/nauticus/controllers/space" + "github.com/edixos/nauticus/controllers/spacetemplate" + + goRuntime "runtime" // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) // to ensure that exec-entrypoint and run can make use of them. _ "k8s.io/client-go/plugin/pkg/client/auth" - nauticusiov1alpha1 "github.com/edixos/nauticus/api/v1alpha1" - "github.com/edixos/nauticus/controllers" "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" clientgoscheme "k8s.io/client-go/kubernetes/scheme" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/healthz" "sigs.k8s.io/controller-runtime/pkg/log/zap" + + nauticusiov1alpha1 "github.com/edixos/nauticus/api/v1alpha1" //+kubebuilder:scaffold:imports ) @@ -91,15 +95,29 @@ func main() { os.Exit(1) } - if err = (&controllers.SpaceReconciler{ - Client: mgr.GetClient(), - Log: ctrl.Log.WithName("controllers").WithName("Space"), - Scheme: mgr.GetScheme(), - Recorder: mgr.GetEventRecorderFor("space-controller"), + if err = (&space.Reconciler{ + Reconciler: shared.Reconciler{ + Client: mgr.GetClient(), + Log: ctrl.Log.WithName("controllers").WithName("Space"), + Scheme: mgr.GetScheme(), + Recorder: mgr.GetEventRecorderFor("space-controller"), + }, }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "Space") os.Exit(1) } + + if err = (&spacetemplate.Reconciler{ + Reconciler: shared.Reconciler{ + Client: mgr.GetClient(), + Log: ctrl.Log.WithName("controllers").WithName("SpaceTemplate"), + Scheme: mgr.GetScheme(), + Recorder: mgr.GetEventRecorderFor("spaceTempalte-controller"), + }, + }).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "SpaceTemplate") + os.Exit(1) + } //+kubebuilder:scaffold:builder if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { diff --git a/mkdocs.yml b/mkdocs.yml index 8bae4fe..9fcc311 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -137,6 +137,8 @@ nav: - Tutorials: - Space Tutorial: tutorials/space-tutorial.md + - Space Template Tutorial: tutorials/space-templates-tutorial.md + - Ignore Underlying Deletion: tutorials/ignore-underlying-deletion.md - Reference: - API Specification: crds-apis.md diff --git a/pkg/controller/constants/constants.go b/pkg/controller/constants/constants.go new file mode 100644 index 0000000..25f30b5 --- /dev/null +++ b/pkg/controller/constants/constants.go @@ -0,0 +1,36 @@ +// Copyright 2022-2023 Edixos +// SPDX-License-Identifier: Apache-2.0 +package constants + +import ( + "time" +) + +const ( + NauticusSpaceFinalizer = "nauticus.io/finalizer" + RequeueAfter = time.Minute * 3 + + SpaceConditionReady string = "Ready" + SpaceConditionCreating string = "Creating" + SpaceConditionFailed string = "Failed" + + SpaceSyncSuccessReason string = "SpaceSyncedSuccessfully" + SpaceCreatingReason string = "SpaceCreating" + SpaceFailedReason string = "SpaceSyncFailed" + + SpaceSyncSuccessMessage string = "Space synced successfully" + SpaceSyncFailMessage string = "Space failed to sync" + SpaceCreatingMessage string = "Creating Space in progress" + + SpaceTplConditionReady string = "Ready" + SpaceTplConditionCreating string = "Creating" + SpaceTplConditionFailed string = "Failed" + + SpaceTplCreatingReason string = "SpaceTemplateCreating" + SpaceTplSyncSuccessReason string = "SpaceTemplateSyncedSuccessfully" + SpaceTplFailedReason string = "SpaceTemplateSyncFailed" + + SpaceTplSyncSuccessMessage string = "SpaceTemplate synced successfully" + SpaceTplCreatingMessage string = "Creating SpaceTemplate in progress" + SpaceTplFailedMessage string = "SpaceTemplate Failed to sync" +) diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 8679174..f0a5f00 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -27,8 +27,28 @@ var ( Help: "In Progress Spaces", }, []string{"name"}, ) + + ReadySpaceTemplates = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "ready_spacetemplates", + Help: "ready SpaceTemplates", + }, []string{"name"}, + ) + + FailedSpaceTemplates = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "not_ready_spacetemplates", + Help: "not ready SpaceTemplates", + }, []string{"name"}, + ) + InProgresSpaceTemplates = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "in_progress_spacetemplates", + Help: "In Progress SpaceTemplates", + }, []string{"name"}, + ) ) func init() { - metrics.Registry.MustRegister(FailedSpaces, ReadySpaces, InProgressSpaces) + metrics.Registry.MustRegister(FailedSpaces, ReadySpaces, InProgressSpaces, ReadySpaceTemplates, FailedSpaceTemplates, InProgresSpaceTemplates) }