diff --git a/PROJECT b/PROJECT index ed17e0fdb..63ee7fe95 100644 --- a/PROJECT +++ b/PROJECT @@ -13,4 +13,13 @@ resources: kind: ClusterExtension path: github.com/operator-framework/operator-controller/api/v1alpha1 version: v1alpha1 +- api: + crdVersion: v1 + namespaced: true + controller: true + domain: operatorframework.io + group: olm + kind: Extension + path: github.com/operator-framework/operator-controller/api/v1alpha1 + version: v1alpha1 version: "3" diff --git a/api/v1alpha1/extension_types.go b/api/v1alpha1/extension_types.go new file mode 100644 index 000000000..eba75fce8 --- /dev/null +++ b/api/v1alpha1/extension_types.go @@ -0,0 +1,133 @@ +/* +Copyright 2024. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1alpha1 + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +type ExtensionManagedState string + +const ( + // Pause resolution of this Extension + ManagedStateActive ExtensionManagedState = "Active" + // Peform resolution of this Extension + ManagedStatePaused ExtensionManagedState = "Paused" +) + +type ExtensionSourcePackage struct { + //+kubebuilder:validation:MaxLength:=48 + //+kubebuilder:validation:Pattern:=^[a-z0-9]+(-[a-z0-9]+)*$ + Name string `json:"name"` + + //+kubebuilder:validation:MaxLength:=64 + //+kubebuilder:validation:Pattern=`^(\s*(=||!=|>|<|>=|=>|<=|=<|~|~>|\^)\s*(v?(0|[1-9]\d*|[x|X|\*])(\.(0|[1-9]\d*|x|X|\*]))?(\.(0|[1-9]\d*|x|X|\*))?(-([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?(\+([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?)\s*)((?:\s+|,\s*|\s*\|\|\s*)(=||!=|>|<|>=|=>|<=|=<|~|~>|\^)\s*(v?(0|[1-9]\d*|x|X|\*])(\.(0|[1-9]\d*|x|X|\*))?(\.(0|[1-9]\d*|x|X|\*]))?(-([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?(\+([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?)\s*)*$` + //+kubebuilder:Optional + // Version is an optional semver constraint on the package version. If not specified, the latest version available of the package will be installed. + // If specified, the specific version of the package will be installed so long as it is available in any of the content sources available. + // Examples: 1.2.3, 1.0.0-alpha, 1.0.0-rc.1 + // + // For more information on semver, please see https://semver.org/ + Version string `json:"version,omitempty"` + + //+kubebuilder:validation:MaxLength:=48 + //+kubebuilder:validation:Pattern:=^[a-z0-9]+([\.-][a-z0-9]+)*$ + // Channel constraint definition + Channel string `json:"channel,omitempty"` +} + +// TODO: Implement ExtensionSourceDirect containing a URL or other reference mechanism + +type ExtensionSource struct { + // A source package defined by a name, version and/or channel + Package *ExtensionSourcePackage `json:"package,omitempty"` + + // A direct package defined by a URL + // TODO: Direct *ExtensionSourceDirect `json:"direct,omitempty"` +} + +// ExtensionSpec defines the desired state of Extension +type ExtensionSpec struct { + //+kubebuilder:validation:Enum:=Active;Paused + //+kubebuilder:default:=Active + //+kubebuilder:Optional + // + // Pause reconciliation on this Extension + Managed ExtensionManagedState `json:"managed,omitempty"` + + //+kubebuilder:validation:MaxLength:=64 + //+kubebuilder:validation:Pattern:=^[a-z0-9]+([\.-][a-z0-9]+)*$ + // + // ServiceAccount name used to install this extension + ServiceAccountName string `json:"serviceAccountName"` + + //+kubebuilder:validation:MaxLength:=64 + //+kubebuilder:validation:Pattern:=^[a-z0-9]+([\.-][a-z0-9]+)*$ + //+kubebuilder:Optional + // + // Location of installation TBD?? + DefaultNamespace string `json:"defaultNamespace,omitempty"` + + // Source of Extension to be installed + Source ExtensionSource `json:"source"` + + //+kubebuilder:validation:Enum:=Enforce;Ignore + //+kubebuilder:default:=Enforce + //+kubebuilder:Optional + // + // Defines the policy for how to handle upgrade constraints + UpgradeConstraintPolicy UpgradeConstraintPolicy `json:"upgradeConstraintPolicy,omitempty"` +} + +// ExtensionStatus defines the observed state of Extension +type ExtensionStatus struct { + // +optional + InstalledBundleResource string `json:"installedBundleResource,omitempty"` + // +optional + ResolvedBundleResource string `json:"resolvedBundleResource,omitempty"` + + // +patchMergeKey=type + // +patchStrategy=merge + // +listType=map + // +listMapKey=type + Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type" protobuf:"bytes,1,rep,name=conditions"` +} + +//+kubebuilder:object:root=true +//+kubebuilder:subresource:status + +// Extension is the Schema for the extensions API +type Extension struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec ExtensionSpec `json:"spec,omitempty"` + Status ExtensionStatus `json:"status,omitempty"` +} + +//+kubebuilder:object:root=true + +// ExtensionList contains a list of Extension +type ExtensionList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []Extension `json:"items"` +} + +func init() { + SchemeBuilder.Register(&Extension{}, &ExtensionList{}) +} diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index d0f54acb6..fbebacf2d 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -126,3 +126,135 @@ func (in *ClusterExtensionStatus) DeepCopy() *ClusterExtensionStatus { in.DeepCopyInto(out) return out } + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *Extension) DeepCopyInto(out *Extension) { + *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 Extension. +func (in *Extension) DeepCopy() *Extension { + if in == nil { + return nil + } + out := new(Extension) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *Extension) 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 *ExtensionList) DeepCopyInto(out *ExtensionList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]Extension, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ExtensionList. +func (in *ExtensionList) DeepCopy() *ExtensionList { + if in == nil { + return nil + } + out := new(ExtensionList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *ExtensionList) 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 *ExtensionSource) DeepCopyInto(out *ExtensionSource) { + *out = *in + if in.Package != nil { + in, out := &in.Package, &out.Package + *out = new(ExtensionSourcePackage) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ExtensionSource. +func (in *ExtensionSource) DeepCopy() *ExtensionSource { + if in == nil { + return nil + } + out := new(ExtensionSource) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ExtensionSourcePackage) DeepCopyInto(out *ExtensionSourcePackage) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ExtensionSourcePackage. +func (in *ExtensionSourcePackage) DeepCopy() *ExtensionSourcePackage { + if in == nil { + return nil + } + out := new(ExtensionSourcePackage) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ExtensionSpec) DeepCopyInto(out *ExtensionSpec) { + *out = *in + in.Source.DeepCopyInto(&out.Source) +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ExtensionSpec. +func (in *ExtensionSpec) DeepCopy() *ExtensionSpec { + if in == nil { + return nil + } + out := new(ExtensionSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ExtensionStatus) DeepCopyInto(out *ExtensionStatus) { + *out = *in + if in.Conditions != nil { + in, out := &in.Conditions, &out.Conditions + *out = make([]v1.Condition, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ExtensionStatus. +func (in *ExtensionStatus) DeepCopy() *ExtensionStatus { + if in == nil { + return nil + } + out := new(ExtensionStatus) + in.DeepCopyInto(out) + return out +} diff --git a/cmd/manager/main.go b/cmd/manager/main.go index 3e872f5f6..df8fa2d49 100644 --- a/cmd/manager/main.go +++ b/cmd/manager/main.go @@ -123,6 +123,13 @@ func main() { setupLog.Error(err, "unable to create controller", "controller", "ClusterExtension") os.Exit(1) } + if err = (&controllers.ExtensionReconciler{ + Client: cl, + Scheme: mgr.GetScheme(), + }).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "Extension") + os.Exit(1) + } //+kubebuilder:scaffold:builder if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { diff --git a/config/crd/bases/olm.operatorframework.io_extensions.yaml b/config/crd/bases/olm.operatorframework.io_extensions.yaml new file mode 100644 index 000000000..b3082ac3c --- /dev/null +++ b/config/crd/bases/olm.operatorframework.io_extensions.yaml @@ -0,0 +1,179 @@ +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.12.0 + name: extensions.olm.operatorframework.io +spec: + group: olm.operatorframework.io + names: + kind: Extension + listKind: ExtensionList + plural: extensions + singular: extension + scope: Namespaced + versions: + - name: v1alpha1 + schema: + openAPIV3Schema: + description: Extension is the Schema for the extensions 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: ExtensionSpec defines the desired state of Extension + properties: + defaultNamespace: + description: Location of installation TBD?? + maxLength: 64 + pattern: ^[a-z0-9]+([\.-][a-z0-9]+)*$ + type: string + managed: + default: Active + description: Pause reconciliation on this Extension + enum: + - Active + - Paused + type: string + serviceAccountName: + description: ServiceAccount name used to install this extension + maxLength: 64 + pattern: ^[a-z0-9]+([\.-][a-z0-9]+)*$ + type: string + source: + description: Source of Extension to be installed + properties: + package: + description: A source package defined by a name, version and/or + channel + properties: + channel: + description: Channel constraint definition + maxLength: 48 + pattern: ^[a-z0-9]+([\.-][a-z0-9]+)*$ + type: string + name: + maxLength: 48 + pattern: ^[a-z0-9]+(-[a-z0-9]+)*$ + type: string + version: + description: "Version is an optional semver constraint on + the package version. If not specified, the latest version + available of the package will be installed. If specified, + the specific version of the package will be installed so + long as it is available in any of the content sources available. + Examples: 1.2.3, 1.0.0-alpha, 1.0.0-rc.1 \n For more information + on semver, please see https://semver.org/" + maxLength: 64 + pattern: ^(\s*(=||!=|>|<|>=|=>|<=|=<|~|~>|\^)\s*(v?(0|[1-9]\d*|[x|X|\*])(\.(0|[1-9]\d*|x|X|\*]))?(\.(0|[1-9]\d*|x|X|\*))?(-([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?(\+([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?)\s*)((?:\s+|,\s*|\s*\|\|\s*)(=||!=|>|<|>=|=>|<=|=<|~|~>|\^)\s*(v?(0|[1-9]\d*|x|X|\*])(\.(0|[1-9]\d*|x|X|\*))?(\.(0|[1-9]\d*|x|X|\*]))?(-([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?(\+([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?)\s*)*$ + type: string + required: + - name + type: object + type: object + upgradeConstraintPolicy: + default: Enforce + description: Defines the policy for how to handle upgrade constraints + enum: + - Enforce + - Ignore + type: string + required: + - serviceAccountName + - source + type: object + status: + description: ExtensionStatus defines the observed state of Extension + properties: + conditions: + 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 + x-kubernetes-list-map-keys: + - type + x-kubernetes-list-type: map + installedBundleResource: + type: string + resolvedBundleResource: + type: string + type: object + type: object + served: true + storage: true + subresources: + status: {} diff --git a/config/rbac/extension_editor_role.yaml b/config/rbac/extension_editor_role.yaml new file mode 100644 index 000000000..1bf3e16d2 --- /dev/null +++ b/config/rbac/extension_editor_role.yaml @@ -0,0 +1,31 @@ +# permissions for end users to edit extensions. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/name: clusterrole + app.kubernetes.io/instance: extension-editor-role + app.kubernetes.io/component: rbac + app.kubernetes.io/created-by: operator-controller + app.kubernetes.io/part-of: operator-controller + app.kubernetes.io/managed-by: kustomize + name: extension-editor-role +rules: +- apiGroups: + - olm.operatorframework.io + resources: + - extensions + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - olm.operatorframework.io + resources: + - extensions/status + verbs: + - get diff --git a/config/rbac/extension_viewer_role.yaml b/config/rbac/extension_viewer_role.yaml new file mode 100644 index 000000000..dbe45d38b --- /dev/null +++ b/config/rbac/extension_viewer_role.yaml @@ -0,0 +1,27 @@ +# permissions for end users to view extensions. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/name: clusterrole + app.kubernetes.io/instance: extension-viewer-role + app.kubernetes.io/component: rbac + app.kubernetes.io/created-by: operator-controller + app.kubernetes.io/part-of: operator-controller + app.kubernetes.io/managed-by: kustomize + name: extension-viewer-role +rules: +- apiGroups: + - olm.operatorframework.io + resources: + - extensions + verbs: + - get + - list + - watch +- apiGroups: + - olm.operatorframework.io + resources: + - extensions/status + verbs: + - get diff --git a/config/rbac/role.yaml b/config/rbac/role.yaml index 5c7c712d9..d75683ff4 100644 --- a/config/rbac/role.yaml +++ b/config/rbac/role.yaml @@ -51,3 +51,29 @@ rules: - get - patch - update +- apiGroups: + - olm.operatorframework.io + resources: + - extensions + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - olm.operatorframework.io + resources: + - extensions/finalizers + verbs: + - update +- apiGroups: + - olm.operatorframework.io + resources: + - extensions/status + verbs: + - get + - patch + - update diff --git a/config/samples/kustomization.yaml b/config/samples/kustomization.yaml index 09aabdc54..bd1783176 100644 --- a/config/samples/kustomization.yaml +++ b/config/samples/kustomization.yaml @@ -1,4 +1,5 @@ ## Append samples of your project ## resources: - olm_v1alpha1_clusterextension.yaml +- olm_v1alpha1_extension.yaml #+kubebuilder:scaffold:manifestskustomizesamples diff --git a/config/samples/olm_v1alpha1_extension.yaml b/config/samples/olm_v1alpha1_extension.yaml new file mode 100644 index 000000000..8f3555391 --- /dev/null +++ b/config/samples/olm_v1alpha1_extension.yaml @@ -0,0 +1,18 @@ +apiVersion: olm.operatorframework.io/v1alpha1 +kind: Extension +metadata: + labels: + app.kubernetes.io/name: extension + app.kubernetes.io/instance: extension-sample + app.kubernetes.io/part-of: operator-controller + app.kubernetes.io/managed-by: kustomize + app.kubernetes.io/created-by: operator-controller + name: extension-sample + namespace: extension-namespace +spec: + paused: false + serviceAccountName: extension-sa + defaultNamespace: extension-sample + package: + name: argocd-operator + version: 0.6.0 diff --git a/internal/controllers/extension_controller.go b/internal/controllers/extension_controller.go new file mode 100644 index 000000000..513874ec6 --- /dev/null +++ b/internal/controllers/extension_controller.go @@ -0,0 +1,62 @@ +/* +Copyright 2022. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package controllers + +import ( + "context" + + "k8s.io/apimachinery/pkg/runtime" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/log" + + ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" +) + +// ExtensionReconciler reconciles a Extension object +type ExtensionReconciler struct { + client.Client + Scheme *runtime.Scheme +} + +//+kubebuilder:rbac:groups=olm.operatorframework.io,resources=extensions,verbs=get;list;watch;create;update;patch;delete +//+kubebuilder:rbac:groups=olm.operatorframework.io,resources=extensions/status,verbs=get;update;patch +//+kubebuilder:rbac:groups=olm.operatorframework.io,resources=extensions/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. +// TODO(user): Modify the Reconcile function to compare the state specified by +// the Extension 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.13.1/pkg/reconcile +func (r *ExtensionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + _ = log.FromContext(ctx) + + // TODO(user): your logic here + + return ctrl.Result{}, nil +} + +// SetupWithManager sets up the controller with the Manager. +func (r *ExtensionReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + For(&ocv1alpha1.Extension{}). + Complete(r) +}