Skip to content

Commit

Permalink
Merge pull request #102 from SomtochiAma/update-event-status
Browse files Browse the repository at this point in the history
Adds Version Check
  • Loading branch information
k8s-ci-robot committed Aug 10, 2020
2 parents 10baa89 + b8ba229 commit a6536ef
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 12 deletions.
9 changes: 0 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34
github.com/go-openapi/jsonreference v0.0.0-20160704190145-13c6e3589ad9/go.mod h1:W3Z9FmVs9qj+KR4zFKmDPGiLdk1D9Rlm7cyMvf57TTg=
github.com/go-openapi/jsonreference v0.17.0/go.mod h1:g4xxGn04lDIRh0GJb5QlpE3HfopLOL6uZrK/VgnsK9I=
github.com/go-openapi/jsonreference v0.18.0/go.mod h1:g4xxGn04lDIRh0GJb5QlpE3HfopLOL6uZrK/VgnsK9I=
github.com/go-openapi/jsonreference v0.19.2 h1:o20suLFB4Ri0tuzpWtyHlh7E7HnkqTNLq6aR6WVNS1w=
github.com/go-openapi/jsonreference v0.19.2/go.mod h1:jMjeRr2HHw6nAVajTXJ4eiUwohSTlpa0o73RUL1owJc=
github.com/go-openapi/jsonreference v0.19.3 h1:5cxNfTy0UVC3X8JL5ymxzyoUZmo8iZb+jeTWn7tUa8o=
github.com/go-openapi/jsonreference v0.19.3/go.mod h1:rjx6GuL8TTa9VaixXglHmQmIL98+wF9xc8zWvFonSJ8=
Expand Down Expand Up @@ -214,7 +213,6 @@ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5a
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/gofuzz v0.0.0-20161122191042-44d81051d367/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI=
github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/gofuzz v1.1.0 h1:Hsa8mG0dQ46ij8Sl2AYJDUv1oA9/d6Vk+3LG99Oe02g=
github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand Down Expand Up @@ -289,7 +287,6 @@ github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e h1:hB2xlXdHp/pmPZq0y3QnmWAArdw9PqbmotexnWx/FU8=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.7.0 h1:aizVhC/NAAcKWb+5QsU1iNOZb4Yws5UO2I+aIprQITM=
github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs=
Expand Down Expand Up @@ -324,12 +321,10 @@ github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn
github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.10.1 h1:q/mM8GF/n0shIN8SaAZ0V+jnLPzen6WIVZdiwrRlMlo=
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.11.0 h1:JAKSXpt1YjtLA7YpPiqO9ss6sNXEsPfSGdwN0UHqzrw=
github.com/onsi/ginkgo v1.11.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.8.1 h1:C5Dqfs/LeauYDX0jJXIe2SWmwCbGzx9yF8C8xy3Lh34=
github.com/onsi/gomega v1.8.1/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoTdcA=
Expand All @@ -346,23 +341,19 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.3 h1:9iH4JKXLzFbOAdtqv/a+j8aewx2Y8lAjAydhbaScPF8=
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
github.com/prometheus/client_golang v1.0.0 h1:vrDKnkGzuGvhNAL56c7DBz29ZL+KxnoR0x7enabFceM=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 h1:S/YWwWx/RA8rT8tKFRuGUZhuA90OyIBpPCXkcbwU8DE=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M=
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
github.com/prometheus/common v0.4.0 h1:7etb9YClo3a6HjLzfl6rIQaU+FDfi0VSX39io3aQ+DM=
github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
github.com/prometheus/common v0.4.1 h1:K0MGApIoQvMw27RTdJkPbr3JZ7DNbtxQNyi5STVM6Kw=
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084 h1:sofwID9zm4tzrgykg80hfFph1mryUeLRsUfoocVVmRY=
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/procfs v0.0.2 h1:6LJUbpNm42llc4HRCuvApCSWB/WfhuNo9K98Q9sNGfs=
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
Expand Down
15 changes: 15 additions & 0 deletions pkg/patterns/addon/pkg/status/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,18 @@ func NewBasic(client client.Client) declarative.Status {
// no preflight checks
}
}

// NewBasicVersionCheck provides an implementation of declarative.Status that
// performs version checks for the version of the operator that the manifest requires.
func NewBasicVersionChecks(client client.Client, version string) (declarative.Status, error) {
v, err := NewVersionCheck(client, version)
if err != nil {
return nil, err
}

return &declarative.StatusBuilder{
ReconciledImpl: NewAggregator(client),
VersionCheckImpl: v,
// no preflight checks
}, nil
}
76 changes: 76 additions & 0 deletions pkg/patterns/addon/pkg/status/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package status

import (
"context"
"fmt"

"github.com/blang/semver"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
addonsv1alpha1 "sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon/pkg/apis/v1alpha1"

"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative"
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/manifest"
)

// NewVersionCheck provides an implementation of declarative.Reconciled that
// checks the version of the operator if it is up to the version required by the manifest
func NewVersionCheck(client client.Client, operatorVersionString string) (*versionCheck, error) {
operatorVersion, err := semver.Parse(operatorVersionString)
if err != nil {
return nil, fmt.Errorf("unable to parse operator version %q: %v", operatorVersionString, err)
}
return &versionCheck{client: client, operatorVersion: operatorVersion}, nil
}

type versionCheck struct {
client client.Client
operatorVersion semver.Version
}

func (p *versionCheck) VersionCheck(
ctx context.Context,
src declarative.DeclarativeObject,
objs *manifest.Objects,
) (bool, error) {
log := log.Log
var minOperatorVersion semver.Version

// Look for annotation from any resource with the max version
for _, obj := range objs.Items {
annotations := obj.UnstructuredObject().GetAnnotations()
if versionNeededStr, ok := annotations["addons.k8s.io/min-operator-version"]; ok {
log.WithValues("min-operator-version", versionNeededStr).Info("Got version requirement addons.k8s.io/operator-version")

versionNeeded, err := semver.Parse(versionNeededStr)
if err != nil {
log.WithValues("version", versionNeededStr).Error(err, "Unable to parse version restriction")
return false, err
}

if versionNeeded.GT(minOperatorVersion) {
minOperatorVersion = versionNeeded
}
}
}

if p.operatorVersion.GTE(minOperatorVersion) {
return true, nil
}

addonObject, ok := src.(addonsv1alpha1.CommonObject)
if !ok {
return false, fmt.Errorf("object %T was not an addonsv1alpha1.CommonObject", src)
}

status := addonsv1alpha1.CommonStatus{
Healthy: false,
Errors: []string{
fmt.Sprintf("manifest needs operator version >= %v, this operator is version %v", minOperatorVersion.String(), p.operatorVersion.String()),
},
}
log.WithValues("name", addonObject.GetName()).WithValues("status", status).Info("updating status")
addonObject.SetCommonStatus(status)

return false, fmt.Errorf("operator not qualified, manifest needs operator version >= %v", minOperatorVersion.String())
}
27 changes: 26 additions & 1 deletion pkg/patterns/declarative/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
recorder "k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/log"
Expand All @@ -52,6 +53,9 @@ type Reconciler struct {

mgr manager.Manager

// recorder is the EventRecorder for creating k8s events
recorder recorder.EventRecorder

options reconcilerParams
}

Expand All @@ -71,6 +75,10 @@ func (r *Reconciler) Init(mgr manager.Manager, prototype DeclarativeObject, opts
r.prototype = prototype
r.kubectl = kubectl

// TODO: Can we derive the name from prototype?
controllerName := "addon-controller"
r.recorder = mgr.GetEventRecorderFor(controllerName)

r.client = mgr.GetClient()
r.config = mgr.GetConfig()
r.mgr = mgr
Expand Down Expand Up @@ -115,7 +123,7 @@ func (r *Reconciler) Reconcile(request reconcile.Request) (result reconcile.Resu
}

if r.options.status != nil {
if err = r.options.status.Preflight(ctx, instance); err != nil {
if err := r.options.status.Preflight(ctx, instance); err != nil {
log.Error(err, "preflight check failed, not reconciling")
return reconcile.Result{}, err
}
Expand All @@ -140,6 +148,23 @@ func (r *Reconciler) reconcileExists(ctx context.Context, name types.NamespacedN
}
log.WithValues("objects", fmt.Sprintf("%d", len(objects.Items))).Info("built deployment objects")

if r.options.status != nil {
isValidVersion, err := r.options.status.VersionCheck(ctx, instance, objects)
if err != nil {
if !isValidVersion {
// r.client isn't exported so can't be updated in version check function
if err := r.client.Status().Update(ctx, instance); err != nil {
return reconcile.Result{}, err
}
r.recorder.Event(instance, "Warning", "Failed version check", err.Error())
log.Error(err, "Version check failed, not reconciling")
return reconcile.Result{}, nil
}
log.Error(err, "Version check failed, trying to reconcile")
return reconcile.Result{}, err
}
}

defer func() {
if r.options.status != nil {
if err := r.options.status.Reconciled(ctx, instance, objects); err != nil {
Expand Down
20 changes: 18 additions & 2 deletions pkg/patterns/declarative/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
type Status interface {
Reconciled
Preflight
VersionCheck
}

type Reconciled interface {
Expand All @@ -42,10 +43,18 @@ type Preflight interface {
Preflight(context.Context, DeclarativeObject) error
}

type VersionCheck interface {
// VersionCheck checks if the version of the operator is greater than or equal to the
// version requested by objects in the manifest, if it isn't it updates the status and
// events and stops reconciling
VersionCheck(context.Context, DeclarativeObject, *manifest.Objects) (bool, error)
}

// StatusBuilder provides a pluggable implementation of Status
type StatusBuilder struct {
ReconciledImpl Reconciled
PreflightImpl Preflight
ReconciledImpl Reconciled
PreflightImpl Preflight
VersionCheckImpl VersionCheck
}

func (s *StatusBuilder) Reconciled(ctx context.Context, src DeclarativeObject, objs *manifest.Objects) error {
Expand All @@ -62,4 +71,11 @@ func (s *StatusBuilder) Preflight(ctx context.Context, src DeclarativeObject) er
return nil
}

func (s *StatusBuilder) VersionCheck(ctx context.Context, src DeclarativeObject, objs *manifest.Objects) (bool, error) {
if s.VersionCheckImpl != nil {
return s.VersionCheckImpl.VersionCheck(ctx, src, objs)
}
return true, nil
}

var _ Status = &StatusBuilder{}

0 comments on commit a6536ef

Please sign in to comment.