diff --git a/api/v1beta1/imagepolicy_types.go b/api/v1beta1/imagepolicy_types.go index 4b811c3f..1f7964c1 100644 --- a/api/v1beta1/imagepolicy_types.go +++ b/api/v1beta1/imagepolicy_types.go @@ -121,7 +121,6 @@ func SetImagePolicyReadiness(p *ImagePolicy, status metav1.ConditionStatus, reas meta.SetResourceCondition(p, meta.ReadyCondition, status, reason, message) } -// +kubebuilder:storageversion // +kubebuilder:object:root=true // +kubebuilder:subresource:status // +kubebuilder:printcolumn:name="LatestImage",type=string,JSONPath=`.status.latestImage` diff --git a/api/v1beta1/imagerepository_types.go b/api/v1beta1/imagerepository_types.go index cb0648f8..de602076 100644 --- a/api/v1beta1/imagerepository_types.go +++ b/api/v1beta1/imagerepository_types.go @@ -132,7 +132,6 @@ func (in ImageRepository) GetTimeout() time.Duration { return duration } -// +kubebuilder:storageversion // +kubebuilder:object:root=true // +kubebuilder:subresource:status // +kubebuilder:printcolumn:name="Last scan",type=string,JSONPath=`.status.lastScanResult.scanTime` diff --git a/api/v1beta2/condition_types.go b/api/v1beta2/condition_types.go new file mode 100644 index 00000000..46ea3581 --- /dev/null +++ b/api/v1beta2/condition_types.go @@ -0,0 +1,22 @@ +/* +Copyright 2020 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 v1beta2 + +const ( + // ImageURLInvalidReason represents the fact that a given repository has an invalid image URL. + ImageURLInvalidReason string = "ImageURLInvalid" +) diff --git a/api/v1beta2/doc.go b/api/v1beta2/doc.go new file mode 100644 index 00000000..e52e90ee --- /dev/null +++ b/api/v1beta2/doc.go @@ -0,0 +1,24 @@ +/* +Copyright 2020 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 v1beta2 contains API types for the image API group, version +// v1beta2. These types are concerned with reflecting metadata from +// OCI image repositories into a cluster, so they can be consulted for +// e.g., automation. +// +// +kubebuilder:object:generate=true +// +groupName=image.toolkit.fluxcd.io +package v1beta2 diff --git a/api/v1beta2/groupversion_info.go b/api/v1beta2/groupversion_info.go new file mode 100644 index 00000000..14c1e9b0 --- /dev/null +++ b/api/v1beta2/groupversion_info.go @@ -0,0 +1,36 @@ +/* +Copyright 2020 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 v1beta2 contains API Schema definitions for the image v1beta2 API group +//+kubebuilder:object:generate=true +//+groupName=image.toolkit.fluxcd.io +package v1beta2 + +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: "image.toolkit.fluxcd.io", Version: "v1beta2"} + + // 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/v1beta2/imagepolicy_types.go b/api/v1beta2/imagepolicy_types.go new file mode 100644 index 00000000..dbfcda38 --- /dev/null +++ b/api/v1beta2/imagepolicy_types.go @@ -0,0 +1,174 @@ +/* +Copyright 2020, 2021 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 v1beta2 + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/fluxcd/pkg/apis/meta" +) + +const ImagePolicyKind = "ImagePolicy" + +// ImagePolicySpec defines the parameters for calculating the +// ImagePolicy +type ImagePolicySpec struct { + // ImageRepositoryRef points at the object specifying the image + // being scanned + // +required + ImageRepositoryRef meta.NamespacedObjectReference `json:"imageRepositoryRef"` + // Policy gives the particulars of the policy to be followed in + // selecting the most recent image + // +required + Policy ImagePolicyChoice `json:"policy"` + // FilterTags enables filtering for only a subset of tags based on a set of + // rules. If no rules are provided, all the tags from the repository will be + // ordered and compared. + // +optional + FilterTags *TagFilter `json:"filterTags,omitempty"` +} + +// ImagePolicyChoice is a union of all the types of policy that can be +// supplied. +type ImagePolicyChoice struct { + // SemVer gives a semantic version range to check against the tags + // available. + // +optional + SemVer *SemVerPolicy `json:"semver,omitempty"` + // Alphabetical set of rules to use for alphabetical ordering of the tags. + // +optional + Alphabetical *AlphabeticalPolicy `json:"alphabetical,omitempty"` + // Numerical set of rules to use for numerical ordering of the tags. + // +optional + Numerical *NumericalPolicy `json:"numerical,omitempty"` +} + +// SemVerPolicy specifies a semantic version policy. +type SemVerPolicy struct { + // Range gives a semver range for the image tag; the highest + // version within the range that's a tag yields the latest image. + // +required + Range string `json:"range"` +} + +// AlphabeticalPolicy specifies a alphabetical ordering policy. +type AlphabeticalPolicy struct { + // Order specifies the sorting order of the tags. Given the letters of the + // alphabet as tags, ascending order would select Z, and descending order + // would select A. + // +kubebuilder:default:="asc" + // +kubebuilder:validation:Enum=asc;desc + // +optional + Order string `json:"order,omitempty"` +} + +// NumericalPolicy specifies a numerical ordering policy. +type NumericalPolicy struct { + // Order specifies the sorting order of the tags. Given the integer values + // from 0 to 9 as tags, ascending order would select 9, and descending order + // would select 0. + // +kubebuilder:default:="asc" + // +kubebuilder:validation:Enum=asc;desc + // +optional + Order string `json:"order,omitempty"` +} + +// TagFilter enables filtering tags based on a set of defined rules +type TagFilter struct { + // Pattern specifies a regular expression pattern used to filter for image + // tags. + // +optional + Pattern string `json:"pattern"` + // Extract allows a capture group to be extracted from the specified regular + // expression pattern, useful before tag evaluation. + // +optional + Extract string `json:"extract"` + // Discriminator allows to split the tags in multiple groups and execute the + // policy against each of those groups. This value if present is a capture + // group to be extracted from the regular expression pattern. + // +optional + Discriminator string `json:"discriminator,omitempty"` + // Additional attributes we want to extract from the tag. + // +optional + Attributes []string `json:"attributes,omitempty"` +} + +// ImagePolicyStatus defines the observed state of ImagePolicy +type ImagePolicyStatus struct { + // LatestImage gives the first in the list of images scanned by + // the image repository, when filtered and ordered according to + // the policy. + LatestImage string `json:"latestImage,omitempty"` + // Distribution of tags scanned by the image repository + // +optional + Distribution map[string]ImageAndAttributes `json:"distribution,omitempty"` + // Discriminator of the latest image. + // +optional + LatestDiscriminator string `json:"latestDiscriminator,omitempty"` + // len of the distribution map. Statistics only. + NbDistribution int `json:"nbDistribution,omitempty"` + // +optional + ObservedGeneration int64 `json:"observedGeneration,omitempty"` + // +optional + Conditions []metav1.Condition `json:"conditions,omitempty"` +} + +type ImageAndAttributes struct { + Image string `json:"image"` + Tag string `json:"tag"` + // +optional + Attributes map[string]string `json:"attributes,omitempty"` +} + +func (p *ImagePolicy) GetStatusConditions() *[]metav1.Condition { + return &p.Status.Conditions +} + +// SetImageRepositoryReadiness sets the ready condition with the given status, reason and message. +func SetImagePolicyReadiness(p *ImagePolicy, status metav1.ConditionStatus, reason, message string) { + p.Status.ObservedGeneration = p.ObjectMeta.Generation + meta.SetResourceCondition(p, meta.ReadyCondition, status, reason, message) +} + +// +kubebuilder:storageversion +// +kubebuilder:object:root=true +// +kubebuilder:subresource:status +// +kubebuilder:printcolumn:name="LatestImage",type=string,JSONPath=`.status.latestImage` +// +kubebuilder:printcolumn:name="Nb distribution",type=string,JSONPath=`.status.nbDistribution` + +// ImagePolicy is the Schema for the imagepolicies API +type ImagePolicy struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec ImagePolicySpec `json:"spec,omitempty"` + // +kubebuilder:default={"observedGeneration":-1} + Status ImagePolicyStatus `json:"status,omitempty"` +} + +// +kubebuilder:object:root=true + +// ImagePolicyList contains a list of ImagePolicy +type ImagePolicyList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []ImagePolicy `json:"items"` +} + +func init() { + SchemeBuilder.Register(&ImagePolicy{}, &ImagePolicyList{}) +} diff --git a/api/v1beta2/imagerepository_types.go b/api/v1beta2/imagerepository_types.go new file mode 100644 index 00000000..8fad1222 --- /dev/null +++ b/api/v1beta2/imagerepository_types.go @@ -0,0 +1,162 @@ +/* +Copyright 2020 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 v1beta2 + +import ( + "time" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/fluxcd/pkg/apis/meta" +) + +const ImageRepositoryKind = "ImageRepository" + +// ImageRepositorySpec defines the parameters for scanning an image +// repository, e.g., `fluxcd/flux`. +type ImageRepositorySpec struct { + // Image is the name of the image repository + // +required + Image string `json:"image,omitempty"` + // Interval is the length of time to wait between + // scans of the image repository. + // +required + Interval metav1.Duration `json:"interval,omitempty"` + + // Timeout for image scanning. + // Defaults to 'Interval' duration. + // +optional + Timeout *metav1.Duration `json:"timeout,omitempty"` + + // SecretRef can be given the name of a secret containing + // credentials to use for the image registry. The secret should be + // created with `kubectl create secret docker-registry`, or the + // equivalent. + // +optional + SecretRef *meta.LocalObjectReference `json:"secretRef,omitempty"` + + // CertSecretRef can be given the name of a secret containing + // either or both of + // + // - a PEM-encoded client certificate (`certFile`) and private + // key (`keyFile`); + // - a PEM-encoded CA certificate (`caFile`) + // + // and whichever are supplied, will be used for connecting to the + // registry. The client cert and key are useful if you are + // authenticating with a certificate; the CA cert is useful if + // you are using a self-signed server certificate. + // +optional + CertSecretRef *meta.LocalObjectReference `json:"certSecretRef,omitempty"` + + // This flag tells the controller to suspend subsequent image scans. + // It does not apply to already started scans. Defaults to false. + // +optional + Suspend bool `json:"suspend,omitempty"` + + // AccessFrom defines an ACL for allowing cross-namespace references + // to the ImageRepository object based on the caller's namespace labels. + // +optional + AccessFrom *AccessFrom `json:"accessFrom,omitempty"` +} + +type AccessFrom struct { + NamespaceSelectors []NamespaceSelector `json:"namespaceSelectors,omitempty"` +} + +type NamespaceSelector struct { + MatchLabels map[string]string `json:"matchLabels,omitempty"` +} + +type ScanResult struct { + TagCount int `json:"tagCount"` + ScanTime metav1.Time `json:"scanTime,omitempty"` +} + +// ImageRepositoryStatus defines the observed state of ImageRepository +type ImageRepositoryStatus struct { + // +optional + Conditions []metav1.Condition `json:"conditions,omitempty"` + + // ObservedGeneration is the last reconciled generation. + // +optional + ObservedGeneration int64 `json:"observedGeneration,omitempty"` + + // CanonicalName is the name of the image repository with all the + // implied bits made explicit; e.g., `docker.io/library/alpine` + // rather than `alpine`. + // +optional + CanonicalImageName string `json:"canonicalImageName,omitempty"` + + // LastScanResult contains the number of fetched tags. + // +optional + LastScanResult *ScanResult `json:"lastScanResult,omitempty"` + + meta.ReconcileRequestStatus `json:",inline"` +} + +// SetImageRepositoryReadiness sets the ready condition with the given status, reason and message. +func SetImageRepositoryReadiness(ir *ImageRepository, status metav1.ConditionStatus, reason, message string) { + ir.Status.ObservedGeneration = ir.ObjectMeta.Generation + meta.SetResourceCondition(ir, meta.ReadyCondition, status, reason, message) +} + +// GetStatusConditions returns a pointer to the Status.Conditions slice +func (in *ImageRepository) GetStatusConditions() *[]metav1.Condition { + return &in.Status.Conditions +} + +// GetTimeout returns the timeout with default. +func (in ImageRepository) GetTimeout() time.Duration { + duration := in.Spec.Interval.Duration + if in.Spec.Timeout != nil { + duration = in.Spec.Timeout.Duration + } + if duration < time.Second { + return time.Second + } + return duration +} + +// +kubebuilder:storageversion +// +kubebuilder:object:root=true +// +kubebuilder:subresource:status +// +kubebuilder:printcolumn:name="Last scan",type=string,JSONPath=`.status.lastScanResult.scanTime` +// +kubebuilder:printcolumn:name="Tags",type=string,JSONPath=`.status.lastScanResult.tagCount` + +// ImageRepository is the Schema for the imagerepositories API +type ImageRepository struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec ImageRepositorySpec `json:"spec,omitempty"` + // +kubebuilder:default={"observedGeneration":-1} + Status ImageRepositoryStatus `json:"status,omitempty"` +} + +// +kubebuilder:object:root=true + +// ImageRepositoryList contains a list of ImageRepository +type ImageRepositoryList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []ImageRepository `json:"items"` +} + +func init() { + SchemeBuilder.Register(&ImageRepository{}, &ImageRepositoryList{}) +} diff --git a/api/v1beta2/zz_generated.deepcopy.go b/api/v1beta2/zz_generated.deepcopy.go new file mode 100644 index 00000000..71521ed5 --- /dev/null +++ b/api/v1beta2/zz_generated.deepcopy.go @@ -0,0 +1,437 @@ +// +build !ignore_autogenerated + +/* +Copyright 2020 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 v1beta2 + +import ( + "github.com/fluxcd/pkg/apis/meta" + "k8s.io/apimachinery/pkg/apis/meta/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 *AccessFrom) DeepCopyInto(out *AccessFrom) { + *out = *in + if in.NamespaceSelectors != nil { + in, out := &in.NamespaceSelectors, &out.NamespaceSelectors + *out = make([]NamespaceSelector, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AccessFrom. +func (in *AccessFrom) DeepCopy() *AccessFrom { + if in == nil { + return nil + } + out := new(AccessFrom) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *AlphabeticalPolicy) DeepCopyInto(out *AlphabeticalPolicy) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AlphabeticalPolicy. +func (in *AlphabeticalPolicy) DeepCopy() *AlphabeticalPolicy { + if in == nil { + return nil + } + out := new(AlphabeticalPolicy) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ImageAndAttributes) DeepCopyInto(out *ImageAndAttributes) { + *out = *in + if in.Attributes != nil { + in, out := &in.Attributes, &out.Attributes + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ImageAndAttributes. +func (in *ImageAndAttributes) DeepCopy() *ImageAndAttributes { + if in == nil { + return nil + } + out := new(ImageAndAttributes) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ImagePolicy) DeepCopyInto(out *ImagePolicy) { + *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 ImagePolicy. +func (in *ImagePolicy) DeepCopy() *ImagePolicy { + if in == nil { + return nil + } + out := new(ImagePolicy) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *ImagePolicy) 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 *ImagePolicyChoice) DeepCopyInto(out *ImagePolicyChoice) { + *out = *in + if in.SemVer != nil { + in, out := &in.SemVer, &out.SemVer + *out = new(SemVerPolicy) + **out = **in + } + if in.Alphabetical != nil { + in, out := &in.Alphabetical, &out.Alphabetical + *out = new(AlphabeticalPolicy) + **out = **in + } + if in.Numerical != nil { + in, out := &in.Numerical, &out.Numerical + *out = new(NumericalPolicy) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ImagePolicyChoice. +func (in *ImagePolicyChoice) DeepCopy() *ImagePolicyChoice { + if in == nil { + return nil + } + out := new(ImagePolicyChoice) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ImagePolicyList) DeepCopyInto(out *ImagePolicyList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]ImagePolicy, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ImagePolicyList. +func (in *ImagePolicyList) DeepCopy() *ImagePolicyList { + if in == nil { + return nil + } + out := new(ImagePolicyList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *ImagePolicyList) 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 *ImagePolicySpec) DeepCopyInto(out *ImagePolicySpec) { + *out = *in + out.ImageRepositoryRef = in.ImageRepositoryRef + in.Policy.DeepCopyInto(&out.Policy) + if in.FilterTags != nil { + in, out := &in.FilterTags, &out.FilterTags + *out = new(TagFilter) + (*in).DeepCopyInto(*out) + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ImagePolicySpec. +func (in *ImagePolicySpec) DeepCopy() *ImagePolicySpec { + if in == nil { + return nil + } + out := new(ImagePolicySpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ImagePolicyStatus) DeepCopyInto(out *ImagePolicyStatus) { + *out = *in + if in.Distribution != nil { + in, out := &in.Distribution, &out.Distribution + *out = make(map[string]ImageAndAttributes, len(*in)) + for key, val := range *in { + (*out)[key] = *val.DeepCopy() + } + } + 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 ImagePolicyStatus. +func (in *ImagePolicyStatus) DeepCopy() *ImagePolicyStatus { + if in == nil { + return nil + } + out := new(ImagePolicyStatus) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ImageRepository) DeepCopyInto(out *ImageRepository) { + *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 ImageRepository. +func (in *ImageRepository) DeepCopy() *ImageRepository { + if in == nil { + return nil + } + out := new(ImageRepository) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *ImageRepository) 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 *ImageRepositoryList) DeepCopyInto(out *ImageRepositoryList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]ImageRepository, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ImageRepositoryList. +func (in *ImageRepositoryList) DeepCopy() *ImageRepositoryList { + if in == nil { + return nil + } + out := new(ImageRepositoryList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *ImageRepositoryList) 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 *ImageRepositorySpec) DeepCopyInto(out *ImageRepositorySpec) { + *out = *in + out.Interval = in.Interval + if in.Timeout != nil { + in, out := &in.Timeout, &out.Timeout + *out = new(v1.Duration) + **out = **in + } + if in.SecretRef != nil { + in, out := &in.SecretRef, &out.SecretRef + *out = new(meta.LocalObjectReference) + **out = **in + } + if in.CertSecretRef != nil { + in, out := &in.CertSecretRef, &out.CertSecretRef + *out = new(meta.LocalObjectReference) + **out = **in + } + if in.AccessFrom != nil { + in, out := &in.AccessFrom, &out.AccessFrom + *out = new(AccessFrom) + (*in).DeepCopyInto(*out) + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ImageRepositorySpec. +func (in *ImageRepositorySpec) DeepCopy() *ImageRepositorySpec { + if in == nil { + return nil + } + out := new(ImageRepositorySpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ImageRepositoryStatus) DeepCopyInto(out *ImageRepositoryStatus) { + *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]) + } + } + if in.LastScanResult != nil { + in, out := &in.LastScanResult, &out.LastScanResult + *out = new(ScanResult) + (*in).DeepCopyInto(*out) + } + out.ReconcileRequestStatus = in.ReconcileRequestStatus +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ImageRepositoryStatus. +func (in *ImageRepositoryStatus) DeepCopy() *ImageRepositoryStatus { + if in == nil { + return nil + } + out := new(ImageRepositoryStatus) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *NamespaceSelector) DeepCopyInto(out *NamespaceSelector) { + *out = *in + if in.MatchLabels != nil { + in, out := &in.MatchLabels, &out.MatchLabels + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NamespaceSelector. +func (in *NamespaceSelector) DeepCopy() *NamespaceSelector { + if in == nil { + return nil + } + out := new(NamespaceSelector) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *NumericalPolicy) DeepCopyInto(out *NumericalPolicy) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NumericalPolicy. +func (in *NumericalPolicy) DeepCopy() *NumericalPolicy { + if in == nil { + return nil + } + out := new(NumericalPolicy) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ScanResult) DeepCopyInto(out *ScanResult) { + *out = *in + in.ScanTime.DeepCopyInto(&out.ScanTime) +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ScanResult. +func (in *ScanResult) DeepCopy() *ScanResult { + if in == nil { + return nil + } + out := new(ScanResult) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *SemVerPolicy) DeepCopyInto(out *SemVerPolicy) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new SemVerPolicy. +func (in *SemVerPolicy) DeepCopy() *SemVerPolicy { + if in == nil { + return nil + } + out := new(SemVerPolicy) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *TagFilter) DeepCopyInto(out *TagFilter) { + *out = *in + if in.Attributes != nil { + in, out := &in.Attributes, &out.Attributes + *out = make([]string, len(*in)) + copy(*out, *in) + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TagFilter. +func (in *TagFilter) DeepCopy() *TagFilter { + if in == nil { + return nil + } + out := new(TagFilter) + in.DeepCopyInto(out) + return out +} diff --git a/config/crd/bases/image.toolkit.fluxcd.io_imagepolicies.yaml b/config/crd/bases/image.toolkit.fluxcd.io_imagepolicies.yaml index 26958985..de4df9ed 100644 --- a/config/crd/bases/image.toolkit.fluxcd.io_imagepolicies.yaml +++ b/config/crd/bases/image.toolkit.fluxcd.io_imagepolicies.yaml @@ -426,6 +426,181 @@ spec: type: object type: object served: true + storage: false + subresources: + status: {} + - additionalPrinterColumns: + - jsonPath: .status.latestImage + name: LatestImage + type: string + - jsonPath: .status.nbDistribution + name: Nb distribution + type: string + name: v1beta2 + schema: + openAPIV3Schema: + description: ImagePolicy is the Schema for the imagepolicies 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: ImagePolicySpec defines the parameters for calculating the ImagePolicy + properties: + filterTags: + description: FilterTags enables filtering for only a subset of tags based on a set of rules. If no rules are provided, all the tags from the repository will be ordered and compared. + properties: + attributes: + description: Additional attributes we want to extract from the tag. + items: + type: string + type: array + discriminator: + description: Discriminator allows to split the tags in multiple groups and execute the policy against each of those groups. This value if present is a capture group to be extracted from the regular expression pattern. + type: string + extract: + description: Extract allows a capture group to be extracted from the specified regular expression pattern, useful before tag evaluation. + type: string + pattern: + description: Pattern specifies a regular expression pattern used to filter for image tags. + type: string + type: object + imageRepositoryRef: + description: ImageRepositoryRef points at the object specifying the image being scanned + properties: + name: + description: Name of the referent + type: string + namespace: + description: Namespace of the referent, when not specified it acts as LocalObjectReference + type: string + required: + - name + type: object + policy: + description: Policy gives the particulars of the policy to be followed in selecting the most recent image + properties: + alphabetical: + description: Alphabetical set of rules to use for alphabetical ordering of the tags. + properties: + order: + default: asc + description: Order specifies the sorting order of the tags. Given the letters of the alphabet as tags, ascending order would select Z, and descending order would select A. + enum: + - asc + - desc + type: string + type: object + numerical: + description: Numerical set of rules to use for numerical ordering of the tags. + properties: + order: + default: asc + description: Order specifies the sorting order of the tags. Given the integer values from 0 to 9 as tags, ascending order would select 9, and descending order would select 0. + enum: + - asc + - desc + type: string + type: object + semver: + description: SemVer gives a semantic version range to check against the tags available. + properties: + range: + description: Range gives a semver range for the image tag; the highest version within the range that's a tag yields the latest image. + type: string + required: + - range + type: object + type: object + required: + - imageRepositoryRef + - policy + type: object + status: + default: + observedGeneration: -1 + description: ImagePolicyStatus defines the observed state of ImagePolicy + 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, 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 + distribution: + additionalProperties: + properties: + attributes: + additionalProperties: + type: string + type: object + image: + type: string + tag: + type: string + required: + - image + - tag + type: object + description: Distribution of tags scanned by the image repository + type: object + latestDiscriminator: + description: Discriminator of the latest image. + type: string + latestImage: + description: LatestImage gives the first in the list of images scanned by the image repository, when filtered and ordered according to the policy. + type: string + nbDistribution: + description: len of the distribution map. Statistics only. + type: integer + observedGeneration: + format: int64 + type: integer + type: object + type: object + served: true storage: true subresources: status: {} diff --git a/config/crd/bases/image.toolkit.fluxcd.io_imagerepositories.yaml b/config/crd/bases/image.toolkit.fluxcd.io_imagerepositories.yaml index 69ad70cd..a085e932 100644 --- a/config/crd/bases/image.toolkit.fluxcd.io_imagerepositories.yaml +++ b/config/crd/bases/image.toolkit.fluxcd.io_imagerepositories.yaml @@ -409,6 +409,148 @@ spec: type: object type: object served: true + storage: false + subresources: + status: {} + - additionalPrinterColumns: + - jsonPath: .status.lastScanResult.scanTime + name: Last scan + type: string + - jsonPath: .status.lastScanResult.tagCount + name: Tags + type: string + name: v1beta2 + schema: + openAPIV3Schema: + description: ImageRepository is the Schema for the imagerepositories 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: ImageRepositorySpec defines the parameters for scanning an image repository, e.g., `fluxcd/flux`. + properties: + accessFrom: + description: AccessFrom defines an ACL for allowing cross-namespace references to the ImageRepository object based on the caller's namespace labels. + properties: + namespaceSelectors: + items: + properties: + matchLabels: + additionalProperties: + type: string + type: object + type: object + type: array + type: object + certSecretRef: + description: "CertSecretRef can be given the name of a secret containing either or both of \n - a PEM-encoded client certificate (`certFile`) and private key (`keyFile`); - a PEM-encoded CA certificate (`caFile`) \n and whichever are supplied, will be used for connecting to the registry. The client cert and key are useful if you are authenticating with a certificate; the CA cert is useful if you are using a self-signed server certificate." + properties: + name: + description: Name of the referent + type: string + required: + - name + type: object + image: + description: Image is the name of the image repository + type: string + interval: + description: Interval is the length of time to wait between scans of the image repository. + type: string + secretRef: + description: SecretRef can be given the name of a secret containing credentials to use for the image registry. The secret should be created with `kubectl create secret docker-registry`, or the equivalent. + properties: + name: + description: Name of the referent + type: string + required: + - name + type: object + suspend: + description: This flag tells the controller to suspend subsequent image scans. It does not apply to already started scans. Defaults to false. + type: boolean + timeout: + description: Timeout for image scanning. Defaults to 'Interval' duration. + type: string + type: object + status: + default: + observedGeneration: -1 + description: ImageRepositoryStatus defines the observed state of ImageRepository + properties: + canonicalImageName: + description: CanonicalName is the name of the image repository with all the implied bits made explicit; e.g., `docker.io/library/alpine` rather than `alpine`. + type: string + 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, 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 + lastHandledReconcileAt: + description: LastHandledReconcileAt holds the value of the most recent reconcile request value, so a change can be detected. + type: string + lastScanResult: + description: LastScanResult contains the number of fetched tags. + properties: + scanTime: + format: date-time + type: string + tagCount: + type: integer + required: + - tagCount + type: object + observedGeneration: + description: ObservedGeneration is the last reconciled generation. + format: int64 + type: integer + type: object + type: object + served: true storage: true subresources: status: {} diff --git a/controllers/imagepolicy_controller.go b/controllers/imagepolicy_controller.go index 981387fd..33cf4117 100644 --- a/controllers/imagepolicy_controller.go +++ b/controllers/imagepolicy_controller.go @@ -18,6 +18,7 @@ package controllers import ( "context" + "encoding/json" "fmt" "time" @@ -41,7 +42,7 @@ import ( "github.com/fluxcd/pkg/runtime/events" "github.com/fluxcd/pkg/runtime/metrics" - imagev1 "github.com/fluxcd/image-reflector-controller/api/v1beta1" + imagev1 "github.com/fluxcd/image-reflector-controller/api/v1beta2" "github.com/fluxcd/image-reflector-controller/internal/policy" ) @@ -162,7 +163,21 @@ func (r *ImagePolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request) return ctrl.Result{}, nil } + if pol.Spec.FilterTags == nil || pol.Spec.FilterTags.Discriminator == "" { + return r.nonDiscriminated(ctx, req, policer, repo, pol) + }else{ + return r.discriminated(ctx, req, policer, repo, pol) + } +} + +func (r *ImagePolicyReconciler) nonDiscriminated(ctx context.Context, req ctrl.Request, policer policy.Policer, repo imagev1.ImageRepository, pol imagev1.ImagePolicy) (ctrl.Result, error) { var latest string + var err error + + pol.Status.Distribution = nil + pol.Status.NbDistribution = 0 + pol.Status.LatestDiscriminator = "" + if policer != nil { var tags []string tags, err = r.Database.Tags(repo.Status.CanonicalImageName) @@ -232,6 +247,108 @@ func (r *ImagePolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request) return ctrl.Result{}, err } +func (r *ImagePolicyReconciler) discriminated(ctx context.Context, req ctrl.Request, policer policy.Policer, repo imagev1.ImageRepository, pol imagev1.ImagePolicy) (ctrl.Result, error) { + distribution := map[string]imagev1.ImageAndAttributes{} + latest := "" + latestDiscriminator := "" + var err error + + listAttributes := []string{ pol.Spec.FilterTags.Discriminator, pol.Spec.FilterTags.Extract } + if len(pol.Spec.FilterTags.Attributes) > 0 { + listAttributes = append(listAttributes, pol.Spec.FilterTags.Attributes...) + } + + if policer != nil { + var tags []string + tags, err = r.Database.Tags(repo.Status.CanonicalImageName) + if err == nil { + var filter *policy.RegexExtractor + filter, err = policy.NewRegexExtractor(pol.Spec.FilterTags.Pattern, listAttributes) + if err == nil { + filter.Apply(tags) + reduced, err := filter.Reduce(pol.Spec.FilterTags.Discriminator, pol.Spec.FilterTags.Extract, policer) + + if err == nil { + distribByExtracted := map[string]string{} + var extractedList []string + + for k, v := range reduced { + distribution[k] = imagev1.ImageAndAttributes{ + Image: repo.Spec.Image, + Tag: v.Tag, + Attributes: v.Attributes, + } + distribByExtracted[v.Extracted] = k + extractedList = append(extractedList, v.Extracted) + } + + newer, err := policer.Latest(extractedList) + + if err == nil { + latestDiscriminator = distribByExtracted[newer] + latest = distribution[latestDiscriminator].Tag + } + } + } + } + } + + + if err != nil { + imagev1.SetImagePolicyReadiness( + &pol, + metav1.ConditionFalse, + meta.ReconciliationFailedReason, + err.Error(), + ) + if err := r.patchStatus(ctx, req, pol.Status); err != nil { + return ctrl.Result{Requeue: true}, err + } + r.event(ctx, pol, events.EventSeverityError, err.Error()) + return ctrl.Result{}, err + } + + if len(distribution) == 0 || latest == "" { + msg := fmt.Sprintf("Cannot determine latest tag for policy: %s", pol.Name) + pol.Status.LatestImage = "" + pol.Status.Distribution = nil + pol.Status.NbDistribution = 0 + imagev1.SetImagePolicyReadiness( + &pol, + metav1.ConditionFalse, + meta.ReconciliationFailedReason, + msg, + ) + + if err := r.patchStatus(ctx, req, pol.Status); err != nil { + return ctrl.Result{}, err + } + r.event(ctx, pol, events.EventSeverityError, msg) + return ctrl.Result{}, nil + } + + distribStr, _ := json.Marshal(distribution) + msg := fmt.Sprintf("Distribution of image for '%s' resolved to: %s", repo.Spec.Image, string(distribStr)) + pol.Status.Distribution = distribution + pol.Status.NbDistribution = len(distribution) + pol.Status.LatestImage = repo.Spec.Image + ":" + latest + pol.Status.LatestDiscriminator = latestDiscriminator + imagev1.SetImagePolicyReadiness( + &pol, + metav1.ConditionTrue, + meta.ReconciliationSucceededReason, + msg, + ) + + if err := r.patchStatus(ctx, req, pol.Status); err != nil { + return ctrl.Result{}, err + } + r.event(ctx, pol, events.EventSeverityInfo, msg) + + return ctrl.Result{}, err +} + + func (r *ImagePolicyReconciler) SetupWithManager(mgr ctrl.Manager, opts ImagePolicyReconcilerOptions) error { // index the policies by which image repo they point at, so that // it's easy to list those out when an image repo changes. diff --git a/controllers/imagerepository_controller.go b/controllers/imagerepository_controller.go index fbc7dcaa..406c34a1 100644 --- a/controllers/imagerepository_controller.go +++ b/controllers/imagerepository_controller.go @@ -56,7 +56,7 @@ import ( "github.com/fluxcd/pkg/runtime/metrics" "github.com/fluxcd/pkg/runtime/predicates" - imagev1 "github.com/fluxcd/image-reflector-controller/api/v1beta1" + imagev1 "github.com/fluxcd/image-reflector-controller/api/v1beta2" ) // These are intended to match the keys used in e.g., diff --git a/controllers/policy_test.go b/controllers/policy_test.go index 0369ef44..1248b502 100644 --- a/controllers/policy_test.go +++ b/controllers/policy_test.go @@ -28,7 +28,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" - imagev1 "github.com/fluxcd/image-reflector-controller/api/v1beta1" + imagev1 "github.com/fluxcd/image-reflector-controller/api/v1beta2" // +kubebuilder:scaffold:imports ) diff --git a/controllers/scan_test.go b/controllers/scan_test.go index ddf57669..e34f5a16 100644 --- a/controllers/scan_test.go +++ b/controllers/scan_test.go @@ -31,7 +31,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" - imagev1 "github.com/fluxcd/image-reflector-controller/api/v1beta1" + imagev1 "github.com/fluxcd/image-reflector-controller/api/v1beta2" // +kubebuilder:scaffold:imports ) diff --git a/controllers/suite_test.go b/controllers/suite_test.go index 670890dd..8a274cba 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -35,7 +35,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/envtest/printer" "sigs.k8s.io/controller-runtime/pkg/log/zap" - imagev1 "github.com/fluxcd/image-reflector-controller/api/v1beta1" + imagev1 "github.com/fluxcd/image-reflector-controller/api/v1beta2" "github.com/fluxcd/image-reflector-controller/internal/database" // +kubebuilder:scaffold:imports ) diff --git a/controllers/tls_test.go b/controllers/tls_test.go index 31987050..eb2f41a3 100644 --- a/controllers/tls_test.go +++ b/controllers/tls_test.go @@ -40,7 +40,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" - imagev1 "github.com/fluxcd/image-reflector-controller/api/v1beta1" + imagev1 "github.com/fluxcd/image-reflector-controller/api/v1beta2" "github.com/fluxcd/pkg/apis/meta" ) diff --git a/internal/policy/extractor.go b/internal/policy/extractor.go new file mode 100644 index 00000000..acca51f3 --- /dev/null +++ b/internal/policy/extractor.go @@ -0,0 +1,116 @@ +/* +Copyright 2021 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 policy + +import ( + "fmt" + "regexp" +) + +// RegexFilter represents a regular expression filter +type RegexExtractor struct { + filtered []AttributeExtracted + + Regexp *regexp.Regexp + Extract []string +} + +type AttributeExtracted struct { + Tag string + Extracted string + Attributes map[string]string +} + +// NewRegexFilter constructs new RegexFilter object +func NewRegexExtractor(pattern string, extract []string) (*RegexExtractor, error) { + m, err := regexp.Compile(pattern) + if err != nil { + return nil, fmt.Errorf("invalid regular expression pattern '%s': %w", pattern, err) + } + return &RegexExtractor{ + Regexp: m, + Extract: extract, + }, nil +} + +// Apply will construct the filtered list of tags based on the provided list of tags +func (e *RegexExtractor) Apply(list []string) { + for _, item := range list { + if submatches := e.Regexp.FindStringSubmatchIndex(item); len(submatches) > 0 { + var extracted = AttributeExtracted{} + extracted.Tag = item + extracted.Attributes = e.extractData(item, submatches) + e.filtered = append(e.filtered, extracted) + } + } +} + +// Extract all data group from the regexp +func (e *RegexExtractor) extractData(item string, submatches []int) map[string]string { + data := map[string]string{} + + for _, field := range e.Extract { + result := []byte{} + result = e.Regexp.ExpandString(result, field, item, submatches) + + key := cleanKey(field) + data[key] = string(result) + } + + return data +} + +func (e *RegexExtractor) Reduce(discriminator string, sorter string, policer Policer) (map[string]AttributeExtracted, error) { + distributedAndExtracted := map[string]map[string]AttributeExtracted{} + + cleanDiscriminator := cleanKey(discriminator) + cleanSorter := cleanKey(sorter) + + for _, tag := range e.filtered { + division := tag.Attributes[cleanDiscriminator] + if _, ok := distributedAndExtracted[division]; !ok { + distributedAndExtracted[division] = map[string]AttributeExtracted{} + } + extracted := tag.Attributes[cleanSorter] + tag.Extracted = extracted + distributedAndExtracted[division][extracted] = tag + } + + ret := map[string]AttributeExtracted{} + for distribution, extracted := range distributedAndExtracted { + var keys []string + for k := range extracted { + keys = append(keys, k) + } + latest, err := policer.Latest(keys) + + if err != nil { + return nil, err + } + ret[distribution] = extracted[latest] + } + + return ret, nil +} + +func cleanKey(field string) string { + key := field + if field[0] == '$' { + key = field[1:] + } + return key +} \ No newline at end of file diff --git a/internal/policy/factory.go b/internal/policy/factory.go index 07e939f2..3d70140f 100644 --- a/internal/policy/factory.go +++ b/internal/policy/factory.go @@ -20,7 +20,7 @@ import ( "fmt" "strings" - imagev1 "github.com/fluxcd/image-reflector-controller/api/v1beta1" + imagev1 "github.com/fluxcd/image-reflector-controller/api/v1beta2" ) // PolicerFromSpec constructs a new policy object based on diff --git a/internal/policy/factory_test.go b/internal/policy/factory_test.go index 2005781e..37d0428f 100644 --- a/internal/policy/factory_test.go +++ b/internal/policy/factory_test.go @@ -19,7 +19,7 @@ package policy import ( "testing" - imagev1 "github.com/fluxcd/image-reflector-controller/api/v1beta1" + imagev1 "github.com/fluxcd/image-reflector-controller/api/v1beta2" ) func TestFactory_PolicerFromSpec(t *testing.T) { diff --git a/main.go b/main.go index 539ec292..0293d6f7 100644 --- a/main.go +++ b/main.go @@ -37,7 +37,7 @@ import ( "github.com/fluxcd/pkg/runtime/pprof" "github.com/fluxcd/pkg/runtime/probes" - imagev1 "github.com/fluxcd/image-reflector-controller/api/v1beta1" + imagev1 "github.com/fluxcd/image-reflector-controller/api/v1beta2" // +kubebuilder:scaffold:imports "github.com/fluxcd/image-reflector-controller/controllers" "github.com/fluxcd/image-reflector-controller/internal/database"