From b277e8ec3a8885ca79076f6e4c9b4990ea85df99 Mon Sep 17 00:00:00 2001 From: Guilhem Lettron Date: Mon, 23 Dec 2024 14:09:39 +0100 Subject: [PATCH] feat: add Artifact object Signed-off-by: Guilhem Lettron --- PROJECT | 3 + api/v1alpha1/artifact_types.go | 87 +++++++++++++++++ api/v1alpha1/groupversion_info.go | 36 +++++++ api/v1alpha1/zz_generated.deepcopy.go | 131 ++++++++++++++++++++++++++ config/crd/kustomization.yaml | 1 + main.go | 2 + 6 files changed, 260 insertions(+) create mode 100644 api/v1alpha1/artifact_types.go create mode 100644 api/v1alpha1/groupversion_info.go create mode 100644 api/v1alpha1/zz_generated.deepcopy.go diff --git a/PROJECT b/PROJECT index 0c243993c..f2abf2cd0 100644 --- a/PROJECT +++ b/PROJECT @@ -40,4 +40,7 @@ resources: - group: source kind: Bucket version: v1 +- group: source + kind: Artifact + version: v1alpha1 version: "2" diff --git a/api/v1alpha1/artifact_types.go b/api/v1alpha1/artifact_types.go new file mode 100644 index 000000000..9ef597b97 --- /dev/null +++ b/api/v1alpha1/artifact_types.go @@ -0,0 +1,87 @@ +/* +Copyright 2024 The Flux authors + +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 ( + "time" + + v1 "github.com/fluxcd/source-controller/api/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +const ( + // BucketKind is the string representation of a Bucket. + ArtifactKind = "Artifact" +) + +// ArtifactSpec defines the desired state of Artifact +type ArtifactSpec struct { + + // +kubebuilder:validation:Required + // +kubebuilder:validation:MinLength=1 + CurrentVersion string `json:"currentVersion,omitempty"` + + // +kubebuilder:validation:Required + Versions map[string]*v1.Artifact `json:"versions,omitempty"` +} + +// ArtifactStatus defines the observed state of Artifact +type ArtifactStatus struct { +} + +// +genclient +// +kubebuilder:storageversion +// +kubebuilder:object:root=true +// +kubebuilder:subresource:status + +// Artifact is the Schema for the artifacts API +type Artifact struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec ArtifactSpec `json:"spec,omitempty"` + Status ArtifactStatus `json:"status,omitempty"` +} + +// GetArtifact returns the latest Artifact from the Artifact if present in +// the status sub-resource. +func (in *Artifact) GetArtifact() *v1.Artifact { + if in.Spec.CurrentVersion == "" { + return nil + } + if in.Spec.Versions == nil { + return nil + } + return in.Spec.Versions[in.Spec.CurrentVersion] +} + +func (in *Artifact) GetRequeueAfter() time.Duration { + return time.Minute +} + +// +kubebuilder:object:root=true + +// ArtifactList contains a list of Artifact +type ArtifactList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []Artifact `json:"items"` +} + +func init() { + SchemeBuilder.Register(&Artifact{}, &ArtifactList{}) +} diff --git a/api/v1alpha1/groupversion_info.go b/api/v1alpha1/groupversion_info.go new file mode 100644 index 000000000..6698fa131 --- /dev/null +++ b/api/v1alpha1/groupversion_info.go @@ -0,0 +1,36 @@ +/* +Copyright 2024 The Flux authors + +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 contains API Schema definitions for the source v1alpha1 API group +// +kubebuilder:object:generate=true +// +groupName=source.toolkit.fluxcd.io +package v1alpha1 + +import ( + "k8s.io/apimachinery/pkg/runtime/schema" + "sigs.k8s.io/controller-runtime/pkg/scheme" +) + +var ( + // GroupVersion is group version used to register these objects + GroupVersion = schema.GroupVersion{Group: "source.toolkit.fluxcd.io", Version: "v1alpha1"} + + // SchemeBuilder is used to add go types to the GroupVersionKind scheme + SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion} + + // AddToScheme adds the types in this group-version to the given scheme. + AddToScheme = SchemeBuilder.AddToScheme +) diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go new file mode 100644 index 000000000..fc752b358 --- /dev/null +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -0,0 +1,131 @@ +//go:build !ignore_autogenerated + +/* +Copyright 2024 The Flux authors + +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. +*/ + +// Code generated by controller-gen. DO NOT EDIT. + +package v1alpha1 + +import ( + "github.com/fluxcd/source-controller/api/v1" + runtime "k8s.io/apimachinery/pkg/runtime" +) + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *Artifact) DeepCopyInto(out *Artifact) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + in.Spec.DeepCopyInto(&out.Spec) + out.Status = in.Status +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Artifact. +func (in *Artifact) DeepCopy() *Artifact { + if in == nil { + return nil + } + out := new(Artifact) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *Artifact) 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 *ArtifactList) DeepCopyInto(out *ArtifactList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]Artifact, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ArtifactList. +func (in *ArtifactList) DeepCopy() *ArtifactList { + if in == nil { + return nil + } + out := new(ArtifactList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *ArtifactList) 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 *ArtifactSpec) DeepCopyInto(out *ArtifactSpec) { + *out = *in + if in.Versions != nil { + in, out := &in.Versions, &out.Versions + *out = make(map[string]*v1.Artifact, len(*in)) + for key, val := range *in { + var outVal *v1.Artifact + if val == nil { + (*out)[key] = nil + } else { + inVal := (*in)[key] + in, out := &inVal, &outVal + *out = new(v1.Artifact) + (*in).DeepCopyInto(*out) + } + (*out)[key] = outVal + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ArtifactSpec. +func (in *ArtifactSpec) DeepCopy() *ArtifactSpec { + if in == nil { + return nil + } + out := new(ArtifactSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ArtifactStatus) DeepCopyInto(out *ArtifactStatus) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ArtifactStatus. +func (in *ArtifactStatus) DeepCopy() *ArtifactStatus { + if in == nil { + return nil + } + out := new(ArtifactStatus) + in.DeepCopyInto(out) + return out +} diff --git a/config/crd/kustomization.yaml b/config/crd/kustomization.yaml index c00716353..379726e2c 100644 --- a/config/crd/kustomization.yaml +++ b/config/crd/kustomization.yaml @@ -6,4 +6,5 @@ resources: - bases/source.toolkit.fluxcd.io_helmcharts.yaml - bases/source.toolkit.fluxcd.io_buckets.yaml - bases/source.toolkit.fluxcd.io_ocirepositories.yaml +- bases/source.toolkit.fluxcd.io_artifacts.yaml # +kubebuilder:scaffold:crdkustomizeresource diff --git a/main.go b/main.go index 42e2f81de..c007ea019 100644 --- a/main.go +++ b/main.go @@ -51,6 +51,7 @@ import ( "github.com/fluxcd/pkg/runtime/probes" "github.com/fluxcd/source-controller/api/v1" + "github.com/fluxcd/source-controller/api/v1alpha1" "github.com/fluxcd/source-controller/api/v1beta2" // +kubebuilder:scaffold:imports @@ -85,6 +86,7 @@ func init() { utilruntime.Must(v1beta2.AddToScheme(scheme)) utilruntime.Must(v1.AddToScheme(scheme)) + utilruntime.Must(v1alpha1.AddToScheme(scheme)) // +kubebuilder:scaffold:scheme }