Skip to content

Commit

Permalink
Consolidate status conditions
Browse files Browse the repository at this point in the history
- Use the same condition type as kubernetes/enhancements#1624 so it can be dropped in favour of the Kubernetes type when that PR is merged
  • Loading branch information
stefanprodan committed Apr 10, 2020
1 parent 9bd597c commit 8cd8d8f
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 104 deletions.
25 changes: 13 additions & 12 deletions api/v1alpha1/condition_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// RepositoryCondition contains condition information for a repository
type RepositoryCondition struct {
// SourceCondition contains condition information for a source
type SourceCondition struct {
// Type of the condition, currently ('Ready').
Type RepositoryConditionType `json:"type"`
// +required
Type string `json:"type"`

// Status of the condition, one of ('True', 'False', 'Unknown').
// +required
Status corev1.ConditionStatus `json:"status"`

// LastTransitionTime is the timestamp corresponding to the last status
// change of this condition.
// +optional
LastTransitionTime *metav1.Time `json:"lastTransitionTime,omitempty"`
// +required
LastTransitionTime metav1.Time `json:"lastTransitionTime,omitempty"`

// Reason is a brief machine readable explanation for the condition's last
// transition.
// +optional
// +required
Reason string `json:"reason,omitempty"`

// Message is a human readable description of the details of the last
Expand All @@ -29,11 +31,10 @@ type RepositoryCondition struct {
Message string `json:"message,omitempty"`
}

// RepositoryConditionType represents an repository condition value
type RepositoryConditionType string

const (
// RepositoryConditionReady represents the fact that a given repository condition
// is in ready state.
RepositoryConditionReady RepositoryConditionType = "Ready"
// ReadyCondition represents the fact that a given source is in ready state.
ReadyCondition string = "Ready"

// InitializingReason represents the fact that a given source is being initialize.
InitializingReason string = "Initializing"
)
2 changes: 1 addition & 1 deletion api/v1alpha1/gitrepository_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type GitRepositorySpec struct {
// GitRepositoryStatus defines the observed state of GitRepository
type GitRepositoryStatus struct {
// +optional
Conditions []RepositoryCondition `json:"conditions,omitempty"`
Conditions []SourceCondition `json:"conditions,omitempty"`

// LastUpdateTime is the timestamp corresponding to the last status
// change of this repository.
Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/helmrepository_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type HelmRepositorySpec struct {
// HelmRepositoryStatus defines the observed state of HelmRepository
type HelmRepositoryStatus struct {
// +optional
Conditions []RepositoryCondition `json:"conditions,omitempty"`
Conditions []SourceCondition `json:"conditions,omitempty"`

// LastUpdateTime is the timestamp corresponding to the last status
// change of this repository.
Expand Down
17 changes: 7 additions & 10 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions config/crd/bases/source.fluxcd.io_gitrepositories.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ spec:
type: string
conditions:
items:
description: RepositoryCondition contains condition information for
a repository
description: SourceCondition contains condition information for a
source
properties:
lastTransitionTime:
description: LastTransitionTime is the timestamp corresponding
Expand Down
7 changes: 3 additions & 4 deletions config/crd/bases/source.fluxcd.io_helmrepositories.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,12 @@ spec:
description: HelmRepositoryStatus defines the observed state of HelmRepository
properties:
artifact:
description: Path to the artifact (index file) of the last repository
sync.
description: Path to the artifact of the last repository index.
type: string
conditions:
items:
description: RepositoryCondition contains condition information for
a repository
description: SourceCondition contains condition information for a
source
properties:
lastTransitionTime:
description: LastTransitionTime is the timestamp corresponding
Expand Down
77 changes: 39 additions & 38 deletions controllers/gitrepository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,9 @@ func (r *GitRepositoryReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
result := ctrl.Result{RequeueAfter: repo.Spec.Interval.Duration}

// set initial status
if r.shouldResetStatus(repo) {
log.Info("Initialising repository")
repo.Status.Artifacts = ""
repo.Status.LastUpdateTime = nil
repo.Status.Conditions = []sourcev1.RepositoryCondition{
{
Type: sourcev1.RepositoryConditionReady,
Status: corev1.ConditionUnknown,
},
}
if reset, status := r.shouldResetStatus(repo); reset {
log.Info("Initializing repository")
repo.Status = status
if err := r.Status().Update(ctx, &repo); err != nil {
log.Error(err, "unable to update GitRepository status")
return result, err
Expand All @@ -97,9 +90,8 @@ func (r *GitRepositoryReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
}

// update status
timeNew := metav1.Now()
readyCondition.LastTransitionTime = &timeNew
repo.Status.Conditions = []sourcev1.RepositoryCondition{readyCondition}
readyCondition.LastTransitionTime = metav1.Now()
repo.Status.Conditions = []sourcev1.SourceCondition{readyCondition}

if err := r.Status().Update(ctx, &repo); err != nil {
log.Error(err, "unable to update GitRepository status")
Expand Down Expand Up @@ -132,7 +124,7 @@ func (r *GitRepositoryReconciler) SetupWithManager(mgr ctrl.Manager) error {
Complete(r)
}

func (r *GitRepositoryReconciler) sync(gr sourcev1.GitRepository) (sourcev1.RepositoryCondition, string, error) {
func (r *GitRepositoryReconciler) sync(gr sourcev1.GitRepository) (sourcev1.SourceCondition, string, error) {
// determine ref
refName := plumbing.NewBranchReferenceName("master")
if gr.Spec.Branch != "" {
Expand All @@ -146,8 +138,8 @@ func (r *GitRepositoryReconciler) sync(gr sourcev1.GitRepository) (sourcev1.Repo
dir, err := ioutil.TempDir("", gr.Name)
if err != nil {
ex := fmt.Errorf("tmp dir error %w", err)
return sourcev1.RepositoryCondition{
Type: sourcev1.RepositoryConditionReady,
return sourcev1.SourceCondition{
Type: sourcev1.ReadyCondition,
Status: corev1.ConditionFalse,
Reason: "ExecFailed",
Message: ex.Error(),
Expand All @@ -165,8 +157,8 @@ func (r *GitRepositoryReconciler) sync(gr sourcev1.GitRepository) (sourcev1.Repo
})
if err != nil {
ex := fmt.Errorf("git clone error %w", err)
return sourcev1.RepositoryCondition{
Type: sourcev1.RepositoryConditionReady,
return sourcev1.SourceCondition{
Type: sourcev1.ReadyCondition,
Status: corev1.ConditionFalse,
Reason: "GitCloneFailed",
Message: ex.Error(),
Expand All @@ -178,8 +170,8 @@ func (r *GitRepositoryReconciler) sync(gr sourcev1.GitRepository) (sourcev1.Repo
rng, err := semver.ParseRange(gr.Spec.SemVer)
if err != nil {
ex := fmt.Errorf("semver parse range error %w", err)
return sourcev1.RepositoryCondition{
Type: sourcev1.RepositoryConditionReady,
return sourcev1.SourceCondition{
Type: sourcev1.ReadyCondition,
Status: corev1.ConditionFalse,
Reason: "GitCloneFailed",
Message: ex.Error(),
Expand All @@ -189,8 +181,8 @@ func (r *GitRepositoryReconciler) sync(gr sourcev1.GitRepository) (sourcev1.Repo
repoTags, err := repo.Tags()
if err != nil {
ex := fmt.Errorf("git list tags error %w", err)
return sourcev1.RepositoryCondition{
Type: sourcev1.RepositoryConditionReady,
return sourcev1.SourceCondition{
Type: sourcev1.ReadyCondition,
Status: corev1.ConditionFalse,
Reason: "GitCloneFailed",
Message: ex.Error(),
Expand Down Expand Up @@ -222,8 +214,8 @@ func (r *GitRepositoryReconciler) sync(gr sourcev1.GitRepository) (sourcev1.Repo
w, err := repo.Worktree()
if err != nil {
ex := fmt.Errorf("git worktree error %w", err)
return sourcev1.RepositoryCondition{
Type: sourcev1.RepositoryConditionReady,
return sourcev1.SourceCondition{
Type: sourcev1.ReadyCondition,
Status: corev1.ConditionFalse,
Reason: "GitCheckoutFailed",
Message: ex.Error(),
Expand All @@ -235,17 +227,17 @@ func (r *GitRepositoryReconciler) sync(gr sourcev1.GitRepository) (sourcev1.Repo
})
if err != nil {
ex := fmt.Errorf("git checkout error %w", err)
return sourcev1.RepositoryCondition{
Type: sourcev1.RepositoryConditionReady,
return sourcev1.SourceCondition{
Type: sourcev1.ReadyCondition,
Status: corev1.ConditionFalse,
Reason: "GitCheckoutFailed",
Message: ex.Error(),
}, "", ex
}
} else {
ex := fmt.Errorf("no match found for semver %s", gr.Spec.SemVer)
return sourcev1.RepositoryCondition{
Type: sourcev1.RepositoryConditionReady,
return sourcev1.SourceCondition{
Type: sourcev1.ReadyCondition,
Status: corev1.ConditionFalse,
Reason: "GitCheckoutFailed",
Message: ex.Error(),
Expand All @@ -257,8 +249,8 @@ func (r *GitRepositoryReconciler) sync(gr sourcev1.GitRepository) (sourcev1.Repo
ref, err := repo.Head()
if err != nil {
ex := fmt.Errorf("git resolve HEAD error %w", err)
return sourcev1.RepositoryCondition{
Type: sourcev1.RepositoryConditionReady,
return sourcev1.SourceCondition{
Type: sourcev1.ReadyCondition,
Status: corev1.ConditionFalse,
Reason: "GitHeadFailed",
Message: ex.Error(),
Expand All @@ -271,8 +263,8 @@ func (r *GitRepositoryReconciler) sync(gr sourcev1.GitRepository) (sourcev1.Repo
err = os.MkdirAll(storage, 0777)
if err != nil {
ex := fmt.Errorf("mkdir dir error %w", err)
return sourcev1.RepositoryCondition{
Type: sourcev1.RepositoryConditionReady,
return sourcev1.SourceCondition{
Type: sourcev1.ReadyCondition,
Status: corev1.ConditionFalse,
Reason: "ExecFailed",
Message: ex.Error(),
Expand All @@ -287,8 +279,8 @@ func (r *GitRepositoryReconciler) sync(gr sourcev1.GitRepository) (sourcev1.Repo
err = command.Run()
if err != nil {
ex := fmt.Errorf("tar %s error %w", artifacts, err)
return sourcev1.RepositoryCondition{
Type: sourcev1.RepositoryConditionReady,
return sourcev1.SourceCondition{
Type: sourcev1.ReadyCondition,
Status: corev1.ConditionFalse,
Reason: "ExecFailed",
Message: ex.Error(),
Expand All @@ -305,15 +297,15 @@ func (r *GitRepositoryReconciler) sync(gr sourcev1.GitRepository) (sourcev1.Repo
artifactsURL := fmt.Sprintf("http://%s/repositories/%s-%s/%s.tar.gz",
hostname, gr.Name, gr.Namespace, ref.Hash().String())

return sourcev1.RepositoryCondition{
Type: sourcev1.RepositoryConditionReady,
return sourcev1.SourceCondition{
Type: sourcev1.ReadyCondition,
Status: corev1.ConditionTrue,
Reason: "GitCloneSucceed",
Message: fmt.Sprintf("Fetched artifacts are available at %s", artifacts),
}, artifactsURL, nil
}

func (r *GitRepositoryReconciler) shouldResetStatus(gr sourcev1.GitRepository) bool {
func (r *GitRepositoryReconciler) shouldResetStatus(gr sourcev1.GitRepository) (bool, sourcev1.GitRepositoryStatus) {
resetStatus := false
if gr.Status.Artifacts != "" {
pathParts := strings.Split(gr.Status.Artifacts, "/")
Expand All @@ -328,5 +320,14 @@ func (r *GitRepositoryReconciler) shouldResetStatus(gr sourcev1.GitRepository) b
resetStatus = true
}

return resetStatus
return resetStatus, sourcev1.GitRepositoryStatus{
Conditions: []sourcev1.SourceCondition{
{
Type: sourcev1.ReadyCondition,
Status: corev1.ConditionUnknown,
Reason: sourcev1.InitializingReason,
LastTransitionTime: metav1.Now(),
},
},
}
}
Loading

0 comments on commit 8cd8d8f

Please sign in to comment.