Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt k8s standardized conditions in apis/meta #44

Merged
merged 3 commits into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions apis/meta/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ import (
"time"
)

type WhateverStatus struct {
type whateverStatus struct {
ReconcileRequestStatus `json:",inline"`
}

type Whatever struct {
type whatever struct {
Annotations map[string]string
Status WhateverStatus `json:"status,omitempty"`
Status whateverStatus `json:"status,omitempty"`
}

func TestGetAnnotationValue(t *testing.T) {
obj := Whatever{
obj := whatever{
Annotations: map[string]string{},
}

Expand Down
127 changes: 0 additions & 127 deletions apis/meta/condition_types.go

This file was deleted.

69 changes: 69 additions & 0 deletions apis/meta/conditions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
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 meta

import (
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
// ReadyCondition is the name of the Ready condition implemented by all toolkit
// resources.
ReadyCondition string = "Ready"
)

const (
// ReconciliationSucceededReason represents the fact that the reconciliation of
// a toolkit resource has succeeded.
ReconciliationSucceededReason string = "ReconciliationSucceeded"

// ReconciliationFailedReason represents the fact that the reconciliation of a
// toolkit resource has failed.
ReconciliationFailedReason string = "ReconciliationFailed"

// ProgressingReason represents the fact that the reconciliation of a toolkit
// resource is underway.
ProgressingReason string = "Progressing"

// DependencyNotReadyReason represents the fact that one of the toolkit resource
// dependencies is not ready.
DependencyNotReadyReason string = "DependencyNotReady"

// SuspendedReason represents the fact that the reconciliation of a toolkit
// resource is suspended.
SuspendedReason string = "Suspended"
)

// InReadyCondition returns if the given Condition slice has a ReadyCondition
// with a 'True' condition status.
func InReadyCondition(conditions []metav1.Condition) bool {
return apimeta.IsStatusConditionTrue(conditions, ReadyCondition)
}

// FilterOutCondition returns a new Condition slice without the Condition of the
// given type.
func FilterOutCondition(conditions []metav1.Condition, conditionType string) []metav1.Condition {
var newConditions []metav1.Condition
for _, c := range conditions {
if c.Type == conditionType {
continue
}
newConditions = append(newConditions, c)
}
return newConditions
}
95 changes: 95 additions & 0 deletions apis/meta/conditions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
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 meta

import (
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestInReadyCondition(t *testing.T) {
var conditions []metav1.Condition

found := InReadyCondition(conditions)
if found {
t.Error("expected InReadyCondition to return false when no conditions")
}

fake := metav1.Condition{
Type: "FakeCondition",
}
conditions = append(conditions, fake)
found = InReadyCondition(conditions)
if found {
t.Error("expected InReadyCondition to return false when no ReadyCondition exists")
}

ready := metav1.Condition{
Type: ReadyCondition,
Status: metav1.ConditionFalse,
}
conditions = append(conditions, ready)
found = InReadyCondition(conditions)
if found {
t.Error("expected InReadyCondition to return false if ReadyCondition Status is false")
}

ready.Status = metav1.ConditionTrue
conditions = []metav1.Condition{ready}
found = InReadyCondition(conditions)
if !found {
t.Error("expected InReadyCondition to return true")
}
}

func TestFilterOutCondition(t *testing.T) {
const FakeCondition = "FakeCondition"
var conditions []metav1.Condition

filtered := FilterOutCondition(conditions, "")
if len(filtered) > 0 {
t.Error("expected FilterOutCondition to return empty slice")
}

fake := metav1.Condition{
Type: FakeCondition,
}
conditions = append(conditions, fake)
filtered = FilterOutCondition(conditions, FakeCondition)
if len(filtered) > 0 {
t.Error("expected FilterOutCondition to return empty slice")
}

ready := metav1.Condition{
Type: ReadyCondition,
}
conditions = append(conditions, ready)
filtered = FilterOutCondition(conditions, FakeCondition)
if filtered[0] != conditions[1] {
t.Error("expected FilterOutCondition to return the ready condition")
}

test := metav1.Condition{
Type: "TestCondition",
}
conditions = append(conditions, test)
filtered = FilterOutCondition(conditions, FakeCondition)
if len(filtered) != 2 {
t.Error("expected FilterOutCondition to have two elements")
}
}
5 changes: 1 addition & 4 deletions apis/meta/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ module github.com/fluxcd/pkg/apis/meta

go 1.15

require (
k8s.io/api v0.19.3
k8s.io/apimachinery v0.19.3
)
require k8s.io/apimachinery v0.19.3
2 changes: 0 additions & 2 deletions apis/meta/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
k8s.io/api v0.19.3 h1:GN6ntFnv44Vptj/b+OnMW7FmzkpDoIDLZRvKX3XH9aU=
k8s.io/api v0.19.3/go.mod h1:VF+5FT1B74Pw3KxMdKyinLo+zynBaMBiAfGMuldcNDs=
k8s.io/apimachinery v0.19.3 h1:bpIQXlKjB4cB/oNpnNnV+BybGPR7iP5oYpsOTEJ4hgc=
k8s.io/apimachinery v0.19.3/go.mod h1:DnPGDnARWFvYa3pMHgSxtbZb7gpzzAZ1pTfaUNDVlmA=
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
Expand Down
6 changes: 3 additions & 3 deletions runtime/metrics/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
corev1 "k8s.io/api/core/v1"

"github.com/fluxcd/pkg/apis/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
Expand Down Expand Up @@ -42,8 +42,8 @@ func (r *Recorder) Collectors() []prometheus.Collector {
return []prometheus.Collector{r.conditionGauge, r.durationHistogram}
}

func (r *Recorder) RecordCondition(ref corev1.ObjectReference, condition meta.Condition, deleted bool) {
for _, status := range []string{string(corev1.ConditionTrue), string(corev1.ConditionFalse), string(corev1.ConditionUnknown), ConditionDeleted} {
func (r *Recorder) RecordCondition(ref corev1.ObjectReference, condition metav1.Condition, deleted bool) {
for _, status := range []string{string(metav1.ConditionTrue), string(metav1.ConditionFalse), string(corev1.ConditionUnknown), ConditionDeleted} {
var value float64
if deleted {
if status == ConditionDeleted {
Expand Down
7 changes: 4 additions & 3 deletions runtime/metrics/recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
corev1 "k8s.io/api/core/v1"

"github.com/fluxcd/pkg/apis/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestRecorder_RecordCondition(t *testing.T) {
Expand All @@ -22,9 +23,9 @@ func TestRecorder_RecordCondition(t *testing.T) {
Name: "test",
}

cond := meta.Condition{
cond := metav1.Condition{
Type: meta.ReadyCondition,
Status: corev1.ConditionTrue,
Status: metav1.ConditionTrue,
}

rec.RecordCondition(ref, cond, false)
Expand All @@ -43,7 +44,7 @@ func TestRecorder_RecordCondition(t *testing.T) {
if *pair.Name == "type" && *pair.Value != meta.ReadyCondition {
t.Errorf("expected condition type to be %s, got %s", meta.ReadyCondition, *pair.Value)
}
if *pair.Name == "status" && *pair.Value == string(corev1.ConditionTrue) {
if *pair.Name == "status" && *pair.Value == string(metav1.ConditionTrue) {
conditionTrueValue = *m.GetGauge().Value
} else if *pair.Name == "status" && *m.GetGauge().Value != 0 {
t.Errorf("expected guage value to be 0, got %v", *m.GetGauge().Value)
Expand Down