Introduce source interface

- add source interface with `GetArtifact` and `GetInterval` funcs
- implement source interface for all types
- fix HelmChart requeue
This commit is contained in:
stefanprodan 2020-04-14 12:40:51 +03:00
parent 95acc78577
commit 1832b2d11d
9 changed files with 54 additions and 23 deletions

View File

@ -126,6 +126,17 @@ func GitRepositoryReadyMessage(repository GitRepository) string {
return "" return ""
} }
// GetArtifact returns the latest artifact from the source
// if present in the status sub-resource.
func (in *GitRepository) GetArtifact() *Artifact {
return in.Status.Artifact
}
// GetInterval returns the interval at which the source is updated.
func (in *GitRepository) GetInterval() metav1.Duration {
return in.Spec.Interval
}
// +kubebuilder:object:root=true // +kubebuilder:object:root=true
// +kubebuilder:subresource:status // +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="URL",type=string,JSONPath=`.spec.url` // +kubebuilder:printcolumn:name="URL",type=string,JSONPath=`.spec.url`

View File

@ -38,18 +38,8 @@ type HelmChartSpec struct {
HelmRepositoryRef corev1.LocalObjectReference `json:"helmRepositoryRef"` HelmRepositoryRef corev1.LocalObjectReference `json:"helmRepositoryRef"`
// The interval at which to check the Helm repository for updates. // The interval at which to check the Helm repository for updates.
// Defaults to the interval of the Helm repository. // +required
// +optional Interval metav1.Duration `json:"interval,omitempty"`
Interval *metav1.Duration `json:"interval,omitempty"`
}
// IntervalOrDefault returns the defined interval on the HelmChartSpec
// or the given default.
func (s HelmChartSpec) IntervalOrDefault(interval metav1.Duration) metav1.Duration {
if s.Interval == nil {
return interval
}
return *s.Interval
} }
// HelmChartStatus defines the observed state of HelmChart // HelmChartStatus defines the observed state of HelmChart
@ -121,6 +111,17 @@ func HelmChartReadyMessage(chart HelmChart) string {
return "" return ""
} }
// GetArtifact returns the latest artifact from the source
// if present in the status sub-resource.
func (in *HelmChart) GetArtifact() *Artifact {
return in.Status.Artifact
}
// GetInterval returns the interval at which the source is updated.
func (in *HelmChart) GetInterval() metav1.Duration {
return in.Spec.Interval
}
// +kubebuilder:object:root=true // +kubebuilder:object:root=true
// +kubebuilder:subresource:status // +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Name",type=string,JSONPath=`.spec.name` // +kubebuilder:printcolumn:name="Name",type=string,JSONPath=`.spec.name`

View File

@ -107,6 +107,17 @@ func HelmRepositoryReadyMessage(repository HelmRepository) string {
return "" return ""
} }
// GetArtifact returns the latest artifact from the source
// if present in the status sub-resource.
func (in *HelmRepository) GetArtifact() *Artifact {
return in.Status.Artifact
}
// GetInterval returns the interval at which the source is updated.
func (in *HelmRepository) GetInterval() metav1.Duration {
return in.Spec.Interval
}
// +kubebuilder:object:root=true // +kubebuilder:object:root=true
// +kubebuilder:subresource:status // +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="URL",type=string,JSONPath=`.spec.url` // +kubebuilder:printcolumn:name="URL",type=string,JSONPath=`.spec.url`

13
api/v1alpha1/source.go Normal file
View File

@ -0,0 +1,13 @@
package v1alpha1
import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
// Source interface must be supported by all API types.
// +k8s:deepcopy-gen=false
type Source interface {
// GetArtifact returns the latest artifact from the source
// if present in the status sub-resource.
GetArtifact() *Artifact
// GetInterval returns the interval at which the source is updated.
GetInterval() metav1.Duration
}

View File

@ -22,7 +22,6 @@ package v1alpha1
import ( import (
"k8s.io/api/core/v1" "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
runtime "k8s.io/apimachinery/pkg/runtime" runtime "k8s.io/apimachinery/pkg/runtime"
) )
@ -174,7 +173,7 @@ func (in *HelmChart) DeepCopyInto(out *HelmChart) {
*out = *in *out = *in
out.TypeMeta = in.TypeMeta out.TypeMeta = in.TypeMeta
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
in.Spec.DeepCopyInto(&out.Spec) out.Spec = in.Spec
in.Status.DeepCopyInto(&out.Status) in.Status.DeepCopyInto(&out.Status)
} }
@ -232,11 +231,7 @@ func (in *HelmChartList) DeepCopyObject() runtime.Object {
func (in *HelmChartSpec) DeepCopyInto(out *HelmChartSpec) { func (in *HelmChartSpec) DeepCopyInto(out *HelmChartSpec) {
*out = *in *out = *in
out.HelmRepositoryRef = in.HelmRepositoryRef out.HelmRepositoryRef = in.HelmRepositoryRef
if in.Interval != nil { out.Interval = in.Interval
in, out := &in.Interval, &out.Interval
*out = new(metav1.Duration)
**out = **in
}
} }
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new HelmChartSpec. // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new HelmChartSpec.

View File

@ -65,7 +65,7 @@ spec:
type: object type: object
interval: interval:
description: The interval at which to check the Helm repository for description: The interval at which to check the Helm repository for
updates. Defaults to the interval of the Helm repository. updates.
type: string type: string
name: name:
description: The name of the Helm chart, as made available by the referenced description: The name of the Helm chart, as made available by the referenced

View File

@ -89,7 +89,7 @@ func (r *GitRepositoryReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
log.Info("Git repository sync succeeded", "msg", sourcev1.GitRepositoryReadyMessage(syncedRepo)) log.Info("Git repository sync succeeded", "msg", sourcev1.GitRepositoryReadyMessage(syncedRepo))
// requeue repository // requeue repository
return ctrl.Result{RequeueAfter: repo.Spec.Interval.Duration}, nil return ctrl.Result{RequeueAfter: repo.GetInterval().Duration}, nil
} }
func (r *GitRepositoryReconciler) SetupWithManager(mgr ctrl.Manager) error { func (r *GitRepositoryReconciler) SetupWithManager(mgr ctrl.Manager) error {

View File

@ -104,7 +104,7 @@ func (r *HelmChartReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
log.Info("Helm chart sync succeeded", "msg", sourcev1.HelmChartReadyMessage(pulledChart)) log.Info("Helm chart sync succeeded", "msg", sourcev1.HelmChartReadyMessage(pulledChart))
// requeue chart // requeue chart
return ctrl.Result{RequeueAfter: repository.Spec.Interval.Duration}, nil return ctrl.Result{RequeueAfter: chart.GetInterval().Duration}, nil
} }
func (r *HelmChartReconciler) SetupWithManager(mgr ctrl.Manager) error { func (r *HelmChartReconciler) SetupWithManager(mgr ctrl.Manager) error {

View File

@ -91,7 +91,7 @@ func (r *HelmRepositoryReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err
log.Info("Helm repository sync succeeded", "msg", sourcev1.HelmRepositoryReadyMessage(syncedRepo)) log.Info("Helm repository sync succeeded", "msg", sourcev1.HelmRepositoryReadyMessage(syncedRepo))
// requeue repository // requeue repository
return ctrl.Result{RequeueAfter: repository.Spec.Interval.Duration}, nil return ctrl.Result{RequeueAfter: repository.GetInterval().Duration}, nil
} }
func (r *HelmRepositoryReconciler) SetupWithManager(mgr ctrl.Manager) error { func (r *HelmRepositoryReconciler) SetupWithManager(mgr ctrl.Manager) error {