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

Allow overriding the operator version in the admin API #2134

Merged
merged 1 commit into from
May 31, 2022
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
1 change: 1 addition & 0 deletions pkg/api/admin/openshiftcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type OpenShiftClusterProperties struct {
LastAdminUpdateError string `json:"lastAdminUpdateError,omitempty"`
MaintenanceTask MaintenanceTask `json:"maintenanceTask,omitempty" mutable:"true"`
OperatorFlags OperatorFlags `json:"operatorFlags,omitempty" mutable:"true"`
OperatorVersion string `json:"operatorVersion,omitempty" mutable:"true"`
CreatedAt time.Time `json:"createdAt,omitempty"`
CreatedBy string `json:"createdBy,omitempty"`
ProvisionedBy string `json:"provisionedBy,omitempty"`
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/admin/openshiftcluster_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (c *openShiftClusterConverter) ToExternal(oc *api.OpenShiftCluster) interfa
LastAdminUpdateError: oc.Properties.LastAdminUpdateError,
MaintenanceTask: MaintenanceTask(oc.Properties.MaintenanceTask),
OperatorFlags: OperatorFlags(oc.Properties.OperatorFlags),
OperatorVersion: oc.Properties.OperatorVersion,
CreatedAt: oc.Properties.CreatedAt,
CreatedBy: oc.Properties.CreatedBy,
ProvisionedBy: oc.Properties.ProvisionedBy,
Expand Down Expand Up @@ -174,6 +175,7 @@ func (c *openShiftClusterConverter) ToInternal(_oc interface{}, out *api.OpenShi
out.Properties.LastAdminUpdateError = oc.Properties.LastAdminUpdateError
out.Properties.MaintenanceTask = api.MaintenanceTask(oc.Properties.MaintenanceTask)
out.Properties.OperatorFlags = api.OperatorFlags(oc.Properties.OperatorFlags)
out.Properties.OperatorVersion = oc.Properties.OperatorVersion
out.Properties.CreatedBy = oc.Properties.CreatedBy
out.Properties.ProvisionedBy = oc.Properties.ProvisionedBy
out.Properties.ClusterProfile.Domain = oc.Properties.ClusterProfile.Domain
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/openshiftcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ type OpenShiftClusterProperties struct {
MaintenanceTask MaintenanceTask `json:"maintenanceTask,omitempty"`

// Operator feature/option flags
OperatorFlags OperatorFlags `json:"operatorFlags,omitempty"`
OperatorFlags OperatorFlags `json:"operatorFlags,omitempty"`
OperatorVersion string `json:"operatorVersion,omitempty"`

CreatedAt time.Time `json:"createdAt,omitempty"`

Expand Down
26 changes: 22 additions & 4 deletions pkg/operator/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ func New(log *logrus.Entry, env env.Interface, oc *api.OpenShiftCluster, arocli
}, nil
}

func (o *operator) resources() ([]kruntime.Object, error) {
// first static resources from Assets
func (o *operator) staticResources() ([]kruntime.Object, error) {
results := []kruntime.Object{}
for _, assetName := range AssetNames() {
b, err := Asset(assetName)
Expand All @@ -97,9 +96,18 @@ func (o *operator) resources() ([]kruntime.Object, error) {
if d.Labels == nil {
d.Labels = map[string]string{}
}
d.Labels["version"] = version.GitCommit
var image string

if o.oc.Properties.OperatorVersion != "" {
image = fmt.Sprintf("%s/aro:%s", o.env.ACRDomain(), o.oc.Properties.OperatorVersion)
d.Labels["version"] = o.oc.Properties.OperatorVersion
} else {
image = o.env.AROOperatorImage()
d.Labels["version"] = version.GitCommit
}

for i := range d.Spec.Template.Spec.Containers {
d.Spec.Template.Spec.Containers[i].Image = o.env.AROOperatorImage()
d.Spec.Template.Spec.Containers[i].Image = image

if o.env.IsLocalDevelopmentMode() {
d.Spec.Template.Spec.Containers[i].Env = append(d.Spec.Template.Spec.Containers[i].Env, corev1.EnvVar{
Expand All @@ -112,6 +120,16 @@ func (o *operator) resources() ([]kruntime.Object, error) {

results = append(results, obj)
}
return results, nil
}

func (o *operator) resources() ([]kruntime.Object, error) {
// first static resources from Assets
results, err := o.staticResources()
if err != nil {
return nil, err
}

// then dynamic resources
key, cert := o.env.ClusterGenevaLoggingSecret()
gcsKeyBytes, err := utiltls.PrivateKeyAsBytes(key)
Expand Down
83 changes: 83 additions & 0 deletions pkg/operator/deploy/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import (
"errors"
"testing"

"github.com/golang/mock/gomock"
appsv1 "k8s.io/api/apps/v1"

"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/util/cmp"
mock_env "github.com/Azure/ARO-RP/pkg/util/mocks/env"
"github.com/Azure/ARO-RP/pkg/util/version"
)

func TestCheckIngressIP(t *testing.T) {
Expand Down Expand Up @@ -124,3 +129,81 @@ func TestCheckIngressIP(t *testing.T) {
})
}
}

func TestOperatorVersion(t *testing.T) {
type test struct {
name string
oc func() *api.OpenShiftClusterProperties
wantVersion string
wantPullspec string
}

for _, tt := range []*test{
{
name: "default",
oc: func() *api.OpenShiftClusterProperties {
return &api.OpenShiftClusterProperties{}
},
wantVersion: version.GitCommit,
wantPullspec: "defaultaroimagefromenv",
},
{
name: "overridden",
oc: func() *api.OpenShiftClusterProperties {
return &api.OpenShiftClusterProperties{
OperatorVersion: "v20220101.0",
}
},
wantVersion: "v20220101.0",
wantPullspec: "intsvcdomain/aro:v20220101.0",
},
} {
t.Run(tt.name, func(t *testing.T) {
oc := tt.oc()

controller := gomock.NewController(t)
defer controller.Finish()

_env := mock_env.NewMockInterface(controller)
_env.EXPECT().ACRDomain().AnyTimes().Return("intsvcdomain")
_env.EXPECT().AROOperatorImage().AnyTimes().Return("defaultaroimagefromenv")
_env.EXPECT().IsLocalDevelopmentMode().AnyTimes().Return(false)

o := &operator{
oc: &api.OpenShiftCluster{Properties: *oc},
env: _env,
}

staticResources, err := o.staticResources()
if err != nil {
t.Error(err)
}

var deployments []*appsv1.Deployment
for _, i := range staticResources {
if d, ok := i.(*appsv1.Deployment); ok {
deployments = append(deployments, d)
}
}

if len(deployments) != 2 {
t.Errorf("found %d deployments, not 2", len(deployments))
}

for _, d := range deployments {
if d.Labels["version"] != tt.wantVersion {
t.Errorf("Got %q, not %q", d.Labels["version"], tt.wantVersion)
}

if len(d.Spec.Template.Spec.Containers) != 1 {
t.Errorf("found %d containers, not 1", len(d.Spec.Template.Spec.Containers))
}

image := d.Spec.Template.Spec.Containers[0].Image
if image != tt.wantPullspec {
t.Errorf("Got %q, not %q", image, tt.wantPullspec)
}
}
})
}
}