Merge pull request #2189 from facchettos/operator-deploy-redo

operator deployment improvement
This commit is contained in:
Amber Brown 2022-06-23 21:37:35 +10:00 коммит произвёл GitHub
Родитель 3e72519b81 cf4067c48e
Коммит 94dd1b76e4
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 97 добавлений и 44 удалений

Просмотреть файл

@ -4,15 +4,16 @@ package deploy
// Licensed under the Apache License 2.0. // Licensed under the Apache License 2.0.
import ( import (
"bytes"
"context" "context"
"embed" "embed"
"errors" "errors"
"fmt" "fmt"
"strings" "strings"
"text/template"
"time" "time"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
extensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" extensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
extensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" extensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
@ -33,7 +34,6 @@ import (
aroclient "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned" aroclient "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned"
"github.com/Azure/ARO-RP/pkg/operator/controllers/genevalogging" "github.com/Azure/ARO-RP/pkg/operator/controllers/genevalogging"
"github.com/Azure/ARO-RP/pkg/util/dynamichelper" "github.com/Azure/ARO-RP/pkg/util/dynamichelper"
utilembed "github.com/Azure/ARO-RP/pkg/util/embed"
"github.com/Azure/ARO-RP/pkg/util/pullsecret" "github.com/Azure/ARO-RP/pkg/util/pullsecret"
"github.com/Azure/ARO-RP/pkg/util/ready" "github.com/Azure/ARO-RP/pkg/util/ready"
"github.com/Azure/ARO-RP/pkg/util/restconfig" "github.com/Azure/ARO-RP/pkg/util/restconfig"
@ -83,49 +83,82 @@ func New(log *logrus.Entry, env env.Interface, oc *api.OpenShiftCluster, arocli
}, nil }, nil
} }
func (o *operator) staticResources() ([]kruntime.Object, error) { type deploymentData struct {
results := []kruntime.Object{} Image string
for _, fileBytes := range utilembed.ReadDirRecursive(embeddedFiles, "staticresources") { Version string
obj, _, err := scheme.Codecs.UniversalDeserializer().Decode(fileBytes, nil, nil) GitCommit string
IsLocalDevelopment bool
HasVersion bool
}
func templateManifests(data deploymentData) ([][]byte, error) {
templatesRoot, err := template.ParseFS(embeddedFiles, "staticresources/*.yaml")
if err != nil {
return nil, err
}
templatesMaster, err := template.ParseFS(embeddedFiles, "staticresources/master/*")
if err != nil {
return nil, err
}
templatesWorker, err := template.ParseFS(embeddedFiles, "staticresources/worker/*")
if err != nil { if err != nil {
return nil, err return nil, err
} }
// set the image for the deployments templatedFiles := make([][]byte, 0)
if d, ok := obj.(*appsv1.Deployment); ok { templatesArray := []*template.Template{templatesMaster, templatesRoot, templatesWorker}
if d.Labels == nil {
d.Labels = map[string]string{}
}
var image string
for _, templates := range templatesArray {
for _, templ := range templates.Templates() {
buff := &bytes.Buffer{}
if err := templ.Execute(buff, data); err != nil {
return nil, err
}
templatedFiles = append(templatedFiles, buff.Bytes())
}
}
return templatedFiles, nil
}
func (o *operator) createDeploymentData() deploymentData {
image := o.env.AROOperatorImage()
hasVersion := false
if o.oc.Properties.OperatorVersion != "" { if o.oc.Properties.OperatorVersion != "" {
image = fmt.Sprintf("%s/aro:%s", o.env.ACRDomain(), o.oc.Properties.OperatorVersion) image = fmt.Sprintf("%s/aro", o.env.ACRDomain())
d.Labels["version"] = o.oc.Properties.OperatorVersion hasVersion = true
} else {
image = o.env.AROOperatorImage()
d.Labels["version"] = version.GitCommit
} }
for i := range d.Spec.Template.Spec.Containers { return deploymentData{
d.Spec.Template.Spec.Containers[i].Image = image IsLocalDevelopment: o.env.IsLocalDevelopmentMode(),
Image: image,
Version: o.oc.Properties.OperatorVersion,
GitCommit: version.GitCommit,
HasVersion: hasVersion,
}
}
if o.env.IsLocalDevelopmentMode() { func (o *operator) createObjects() ([]kruntime.Object, error) {
d.Spec.Template.Spec.Containers[i].Env = append(d.Spec.Template.Spec.Containers[i].Env, corev1.EnvVar{ deploymentData := o.createDeploymentData()
Name: "RP_MODE", templated, err := templateManifests(deploymentData)
Value: "development", if err != nil {
}) return nil, err
} }
objects := make([]kruntime.Object, 0, len(templated))
for _, v := range templated {
obj, _, err := scheme.Codecs.UniversalDeserializer().Decode(v, nil, nil)
if err != nil {
return nil, err
} }
objects = append(objects, obj)
} }
results = append(results, obj) return objects, nil
}
return results, nil
} }
func (o *operator) resources() ([]kruntime.Object, error) { func (o *operator) resources() ([]kruntime.Object, error) {
// first static resources from Assets // first static resources from Assets
results, err := o.staticResources()
results, err := o.createObjects()
if err != nil { if err != nil {
return nil, err return nil, err
} }

Просмотреть файл

@ -174,7 +174,7 @@ func TestOperatorVersion(t *testing.T) {
env: _env, env: _env,
} }
staticResources, err := o.staticResources() staticResources, err := o.createObjects()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -192,7 +192,7 @@ func TestOperatorVersion(t *testing.T) {
for _, d := range deployments { for _, d := range deployments {
if d.Labels["version"] != tt.wantVersion { if d.Labels["version"] != tt.wantVersion {
t.Errorf("Got %q, not %q", d.Labels["version"], tt.wantVersion) t.Errorf("Got %q, not %q for label \"version\"", d.Labels["version"], tt.wantVersion)
} }
if len(d.Spec.Template.Spec.Containers) != 1 { if len(d.Spec.Template.Spec.Containers) != 1 {
@ -201,7 +201,7 @@ func TestOperatorVersion(t *testing.T) {
image := d.Spec.Template.Spec.Containers[0].Image image := d.Spec.Template.Spec.Containers[0].Image
if image != tt.wantPullspec { if image != tt.wantPullspec {
t.Errorf("Got %q, not %q", image, tt.wantPullspec) t.Errorf("Got %q, not %q for the image", image, tt.wantPullspec)
} }
} }
}) })

Просмотреть файл

@ -3,6 +3,11 @@ kind: Deployment
metadata: metadata:
labels: labels:
app: aro-operator-master app: aro-operator-master
{{ if .HasVersion}}
version: {{ .Version }}
{{ else }}
version: unknown
{{end}}
name: aro-operator-master name: aro-operator-master
namespace: openshift-azure-operator namespace: openshift-azure-operator
spec: spec:
@ -25,8 +30,13 @@ spec:
args: args:
- operator - operator
- master - master
image: GENERATED image: "{{ .Image }}{{ if .HasVersion}}:{{ if .IsLocalDevelopment }}{{ .GitCommit }}{{ else }}{{ .Version }}{{end}}{{end}}"
name: aro-operator name: aro-operator
{{ if .IsLocalDevelopment}}
env:
- name: "RP_MODE"
value: "development"
{{ end }}
ports: ports:
- containerPort: 8080 - containerPort: 8080
name: http name: http

Просмотреть файл

@ -3,6 +3,11 @@ kind: Deployment
metadata: metadata:
labels: labels:
app: aro-operator-worker app: aro-operator-worker
{{ if .HasVersion}}
version: {{ .Version }}
{{ else }}
version: unknown
{{end}}
name: aro-operator-worker name: aro-operator-worker
namespace: openshift-azure-operator namespace: openshift-azure-operator
spec: spec:
@ -25,7 +30,12 @@ spec:
args: args:
- operator - operator
- worker - worker
image: GENERATED image: "{{ .Image }}{{ if .HasVersion}}:{{ if .IsLocalDevelopment }}{{ .GitCommit }}{{ else }}{{ .Version }}{{end}}{{end}}"
{{ if .IsLocalDevelopment}}
env:
- name: "RP_MODE"
value: "development"
{{ end }}
name: aro-operator name: aro-operator
livenessProbe: livenessProbe:
httpGet: httpGet: