@@ -3,26 +3,29 @@ package status
3
3
import (
4
4
"context"
5
5
"fmt"
6
+
6
7
"github.com/blang/semver"
7
8
"sigs.k8s.io/controller-runtime/pkg/client"
8
9
"sigs.k8s.io/controller-runtime/pkg/log"
9
10
addonsv1alpha1 "sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon/pkg/apis/v1alpha1"
10
11
11
- //"sigs.k8s.io/controller-runtime/pkg/log"
12
-
13
12
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative"
14
13
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/manifest"
15
14
)
16
15
17
16
// NewVersionCheck provides an implementation of declarative.Reconciled that
18
17
// checks the version of the operator if it is up to the version required by the manifest
19
- func NewVersionCheck (client client.Client , version string ) * versionCheck {
20
- return & versionCheck {client , version }
18
+ func NewVersionCheck (client client.Client , operatorVersionString string ) (* versionCheck , error ) {
19
+ operatorVersion , err := semver .Parse (operatorVersionString )
20
+ if err != nil {
21
+ return nil , fmt .Errorf ("unable to parse operator version %q: %v" , operatorVersionString , err )
22
+ }
23
+ return & versionCheck {client : client , operatorVersion : operatorVersion }, nil
21
24
}
22
25
23
26
type versionCheck struct {
24
- client client.Client
25
- version string
27
+ client client.Client
28
+ operatorVersion semver. Version
26
29
}
27
30
28
31
func (p * versionCheck ) VersionCheck (
@@ -31,39 +34,27 @@ func (p *versionCheck) VersionCheck(
31
34
objs * manifest.Objects ,
32
35
) (bool , error ) {
33
36
log := log .Log
34
- zeroVersion := semver.Version {}
35
- var versionNeededStr string
36
- maxVersion := zeroVersion
37
+ var minOperatorVersion semver.Version
37
38
38
39
// Look for annotation from any resource with the max version
39
40
for _ , obj := range objs .Items {
40
- unstruct := obj .UnstructuredObject ().Object
41
- metadata := unstruct ["metadata" ].(map [string ]interface {})
42
- annotations , ok := metadata ["annotations" ].(map [string ]interface {})
43
- if ok {
44
- versionNeededStr , _ = annotations ["addons.k8s.io/operator-version" ].(string )
45
- log .WithValues ("version" , versionNeededStr ).Info ("Got version, %v" )
41
+ annotations := obj .UnstructuredObject ().GetAnnotations ()
42
+ if versionNeededStr , ok := annotations ["addons.k8s.io/min-operator-version" ]; ok {
43
+ log .WithValues ("min-operator-version" , versionNeededStr ).Info ("Got version requirement addons.k8s.io/operator-version" )
46
44
47
- versionActual , err := semver .Make (versionNeededStr )
45
+ versionNeeded , err := semver .Parse (versionNeededStr )
48
46
if err != nil {
49
- log .WithValues ("version" , versionNeededStr ).Info ( "Unable to convert string to version, skipping this object " )
50
- continue
47
+ log .WithValues ("version" , versionNeededStr ).Error ( err , "Unable to parse version restriction " )
48
+ return false , err
51
49
}
52
50
53
- if versionActual .GT (maxVersion ) {
54
- maxVersion = versionActual
51
+ if versionNeeded .GT (minOperatorVersion ) {
52
+ minOperatorVersion = versionNeeded
55
53
}
56
54
}
57
55
}
58
56
59
- // TODO(somtochi): Do we want to return an error when the version is invalid or just skip and use the operator?
60
- operatorVersion , err := semver .Make (p .version )
61
- if err != nil {
62
- log .WithValues ("version" , p .version ).Info ("Unable to convert string to version, skipping check" )
63
- return true , nil
64
- }
65
-
66
- if maxVersion .Equals (zeroVersion ) || ! maxVersion .GT (operatorVersion ) {
57
+ if p .operatorVersion .GTE (minOperatorVersion ) {
67
58
return true , nil
68
59
}
69
60
@@ -75,11 +66,11 @@ func (p *versionCheck) VersionCheck(
75
66
status := addonsv1alpha1.CommonStatus {
76
67
Healthy : false ,
77
68
Errors : []string {
78
- fmt .Sprintf ("Addons needs version %v, this operator is version %v" , maxVersion .String (), operatorVersion .String ()),
69
+ fmt .Sprintf ("manifest needs operator version >= %v, this operator is version %v" , minOperatorVersion .String (), p . operatorVersion .String ()),
79
70
},
80
71
}
81
72
log .WithValues ("name" , addonObject .GetName ()).WithValues ("status" , status ).Info ("updating status" )
82
73
addonObject .SetCommonStatus (status )
83
74
84
- return false , fmt .Errorf ("operator not qualified, manifest needs operator >= %v" , maxVersion .String ())
75
+ return false , fmt .Errorf ("operator not qualified, manifest needs operator version >= %v" , minOperatorVersion .String ())
85
76
}
0 commit comments