Skip to content

Commit

Permalink
Changes annotations merge (#149)
Browse files Browse the repository at this point in the history
* update release docs

* changes annotations merge with labels.Merge,
changes return for vmcluster annotations

* changes order of annotations merge
old values always wins, it makes annotation values immutable for operator,
only user can change it.
  • Loading branch information
f41gh7 committed Jan 20, 2021
1 parent c4b68d2 commit 771605e
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 108 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ packagemanifests: manifests fix118 fix_crd_nulls
mv packagemanifests/$(VERSION_TRIM)/victoriametrics-operator.clusterserviceversion.yaml packagemanifests/$(VERSION_TRIM)/victoriametrics-operator.$(VERSION_TRIM).clusterserviceversion.yaml
sed -i "s|$(DOCKER_REPO):.*|$(DOCKER_REPO):$(VERSION)|" packagemanifests/$(VERSION_TRIM)/*
docker run --rm -v "${PWD}":/workdir mikefarah/yq:2.2.0 \
yq m -i -a=append packagemanifests/$(VERSION_TRIM)/victoriametrics-operator.$(VERSION_TRIM).clusterserviceversion.yaml hack/bundle_csv_vmagent.yaml
yq m -i -a packagemanifests/$(VERSION_TRIM)/victoriametrics-operator.$(VERSION_TRIM).clusterserviceversion.yaml hack/bundle_csv_vmagent.yaml


packagemanifests-push: packagemanifests
Expand Down
51 changes: 51 additions & 0 deletions Release.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
### Helm charts release

#### Bump the version of images.

1. Need to update [`values.yaml`](https://github.com/VictoriaMetrics/helm-charts/blob/master/charts/victoria-metrics-operator/values.yaml),
2. Specify the correct version in [`Chart.yaml`](https://github.com/VictoriaMetrics/helm-charts/blob/master/charts/victoria-metrics-operator/Chart.yaml)
3. Update version [README.md](https://github.com/VictoriaMetrics/helm-charts/blob/master/charts/victoria-metrics-operator/README.md), specify the new version in the documentation
4. Push changes to master. `master` is a source of truth
5. Rebase `master` into `gh-pages` branch
6. Run `make package` which creates or updates zip file with the packed chart
7. Run `make merge`. It creates or updates metadata for charts in index.yaml
8. Push the changes to `gh-pages` branch


### Operator Hub release

checkout to the latest release:
1) `git checkout tags/v0.7.2`
2) build package manifest: `TAG=v0.7.2 make packagemanifests`
3) add replacement for a previous version to generated cluster csv:
`vi packagemanifests/0.7.2/victoriametrics-operator.0.7.2.clusterserviceversion.yaml`
```yaml
spec:
replaces: victoriametrics-operator.v0.6.1
```
4) publish changes to the quay, login first with your login, password:
`bash hack/get_quay_token.sh`, copy token content to var `export AUTH_TOKEN="basic afsASF"`,
then push change to quay: `TAG=v0.7.2 make packagemanifests-push`

5) now you have to copy content of packagemanifests to the community-operators repo,
first for upstream, next for community, sign-off commits and create PRs.
```bash
cp -R packagemanifests/* ~/community-operators/upstream-community-operators/victoriametrics-operator
cd ~/community-operators
git add upstream-community-operators/victoriametrics-operator/0.7.2/
git add upstream-community-operators/victoriametrics-operator/victoriametrics-operator.package.yaml
git commit -m "updates victoriametrics operator version 0.7.2
Signed-off-by: Nikolay Khramchikhin <nik@victoriametrics.com>" --signoff
```
checkout to the new branch and create separate commit for openshift operator-hub
```bash
cp -R packagemanifests/* ~/community-operators/community-operators/victoriametrics-operator
cd ~/community-operators
git add community-operators/victoriametrics-operator/0.7.2/
git add community-operators/victoriametrics-operator/victoriametrics-operator.package.yaml
git commit -m "updates victoriametrics operator version 0.7.2
Signed-off-by: Nikolay Khramchikhin <nik@victoriametrics.com>" --signoff
```

6) create pull requests at community-operator repo:
https://github.com/operator-framework/community-operators
6 changes: 3 additions & 3 deletions api/v1beta1/vmcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,21 +641,21 @@ func (cr VMCluster) FinalLabels(baseLabels map[string]string) map[string]string

func (cr VMCluster) VMSelectPodAnnotations() map[string]string {
if cr.Spec.VMSelect == nil || cr.Spec.VMSelect.PodMetadata == nil {
return nil
return make(map[string]string)
}
return cr.Spec.VMSelect.PodMetadata.Annotations
}

func (cr VMCluster) VMInsertPodAnnotations() map[string]string {
if cr.Spec.VMInsert == nil || cr.Spec.VMInsert.PodMetadata == nil {
return nil
return make(map[string]string)
}
return cr.Spec.VMInsert.PodMetadata.Annotations
}

func (cr VMCluster) VMStoragePodAnnotations() map[string]string {
if cr.Spec.VMStorage == nil || cr.Spec.VMStorage.PodMetadata == nil {
return nil
return make(map[string]string)
}
return cr.Spec.VMStorage.PodMetadata.Annotations
}
Expand Down
13 changes: 4 additions & 9 deletions controllers/factory/alertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -89,12 +90,8 @@ func CreateOrUpdateAlertManager(ctx context.Context, cr *victoriametricsv1beta1.
}

func updateStsForAlertManager(ctx context.Context, rclient client.Client, oldSts, newSts *appsv1.StatefulSet) error {
for k, v := range oldSts.Annotations {
newSts.Annotations[k] = v
}
for k, v := range oldSts.Spec.Template.Annotations {
newSts.Spec.Template.Annotations[k] = v
}
newSts.Annotations = labels.Merge(newSts.Annotations, oldSts.Annotations)
newSts.Spec.Template.Annotations = labels.Merge(newSts.Spec.Template.Annotations, oldSts.Spec.Template.Annotations)
// hack for break reconcile loop at kubernetes 1.18
newSts.Status.Replicas = oldSts.Status.Replicas

Expand Down Expand Up @@ -208,9 +205,7 @@ func CreateOrUpdateAlertManagerService(ctx context.Context, cr *victoriametricsv
return nil, fmt.Errorf("cannot get service for vmalertmanager sts: %w", err)
}
}
for annotation, value := range oldService.Annotations {
newService.Annotations[annotation] = value
}
newService.Annotations = labels.Merge(newService.Annotations, oldService.Annotations)
if oldService.Spec.ClusterIP != "" {
newService.Spec.ClusterIP = oldService.Spec.ClusterIP
}
Expand Down
4 changes: 2 additions & 2 deletions controllers/factory/psp/psp.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func ensureClusterRoleExists(ctx context.Context, cr CRDObject, rclient client.C
}

existsClusterRole.Labels = labels.Merge(existsClusterRole.Labels, clusterRole.Labels)
existsClusterRole.Annotations = labels.Merge(existsClusterRole.Annotations, clusterRole.Labels)
existsClusterRole.Annotations = labels.Merge(clusterRole.Annotations, existsClusterRole.Annotations)
return rclient.Update(ctx, &existsClusterRole)
}

Expand All @@ -134,7 +134,7 @@ func ensureClusterRoleBindingExists(ctx context.Context, cr CRDObject, rclient c
}

existsClusterRoleBinding.Labels = labels.Merge(existsClusterRoleBinding.Labels, clusterRoleBinding.Labels)
existsClusterRoleBinding.Annotations = labels.Merge(existsClusterRoleBinding.Annotations, clusterRoleBinding.Labels)
existsClusterRoleBinding.Annotations = labels.Merge(clusterRoleBinding.Annotations, existsClusterRoleBinding.Annotations)
return rclient.Update(ctx, clusterRoleBinding)
}

Expand Down
29 changes: 13 additions & 16 deletions controllers/factory/vmagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"sort"
"strings"

"k8s.io/apimachinery/pkg/labels"

"github.com/VictoriaMetrics/operator/controllers/factory/k8stools"

"github.com/VictoriaMetrics/operator/controllers/factory/vmagent"
Expand Down Expand Up @@ -37,38 +39,37 @@ const (

func CreateOrUpdateVMAgentService(ctx context.Context, cr *victoriametricsv1beta1.VMAgent, rclient client.Client, c *config.BaseOperatorConf) (*corev1.Service, error) {
l := log.WithValues("recon.vm.service.name", cr.Name)
NewService := newServiceVMAgent(cr, c)
newService := newServiceVMAgent(cr, c)

currentService := &corev1.Service{}
err := rclient.Get(ctx, types.NamespacedName{Namespace: cr.Namespace, Name: NewService.Name}, currentService)
err := rclient.Get(ctx, types.NamespacedName{Namespace: cr.Namespace, Name: newService.Name}, currentService)
if err != nil {
if errors.IsNotFound(err) {
l.Info("creating new service for vm agent")
err := rclient.Create(ctx, NewService)
err := rclient.Create(ctx, newService)
if err != nil {
return nil, fmt.Errorf("cannot create new service for vmagent: %w", err)
}
} else {
return nil, fmt.Errorf("cannot get vmagent service for reconcile: %w", err)
}
}
for annotation, value := range currentService.Annotations {
NewService.Annotations[annotation] = value
}
newService.Annotations = labels.Merge(newService.Annotations, currentService.Annotations)

if currentService.Spec.ClusterIP != "" {
NewService.Spec.ClusterIP = currentService.Spec.ClusterIP
newService.Spec.ClusterIP = currentService.Spec.ClusterIP
}
if currentService.ResourceVersion != "" {
NewService.ResourceVersion = currentService.ResourceVersion
newService.ResourceVersion = currentService.ResourceVersion
}
err = rclient.Update(ctx, NewService)
err = rclient.Update(ctx, newService)
if err != nil {
l.Error(err, "cannot update vmagent service")
return nil, err
}

l.Info("vmagent service reconciled")
return NewService, nil
return newService, nil
}

func newServiceVMAgent(cr *victoriametricsv1beta1.VMAgent, c *config.BaseOperatorConf) *corev1.Service {
Expand Down Expand Up @@ -160,12 +161,8 @@ func CreateOrUpdateVMAgent(ctx context.Context, cr *victoriametricsv1beta1.VMAge
}
}
l.Info("updating vmagent")
for annotation, value := range currentDeploy.Annotations {
newDeploy.Annotations[annotation] = value
}
for annotation, value := range currentDeploy.Spec.Template.Annotations {
newDeploy.Spec.Template.Annotations[annotation] = value
}
newDeploy.Annotations = labels.Merge(newDeploy.Annotations, currentDeploy.Annotations)
newDeploy.Spec.Template.Annotations = labels.Merge(newDeploy.Spec.Template.Annotations, currentDeploy.Spec.Template.Annotations)

err = rclient.Update(ctx, newDeploy)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions controllers/factory/vmagent/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func ensureVMAgentCRExist(ctx context.Context, cr *v1beta12.VMAgent, rclient cli
}

existsClusterRole.Labels = labels.Merge(existsClusterRole.Labels, clusterRole.Labels)
existsClusterRole.Annotations = labels.Merge(existsClusterRole.Annotations, clusterRole.Labels)
existsClusterRole.Annotations = labels.Merge(clusterRole.Annotations, existsClusterRole.Annotations)
return rclient.Update(ctx, &existsClusterRole)
}

Expand All @@ -71,7 +71,7 @@ func ensureVMAgentCRBExist(ctx context.Context, cr *v1beta12.VMAgent, rclient cl
}

existsClusterRoleBinding.Labels = labels.Merge(existsClusterRoleBinding.Labels, clusterRoleBinding.Labels)
existsClusterRoleBinding.Annotations = labels.Merge(existsClusterRoleBinding.Annotations, clusterRoleBinding.Labels)
existsClusterRoleBinding.Annotations = labels.Merge(clusterRoleBinding.Annotations, existsClusterRoleBinding.Annotations)
return rclient.Update(ctx, clusterRoleBinding)
}

Expand Down
15 changes: 5 additions & 10 deletions controllers/factory/vmalert.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"sort"
"strings"

"k8s.io/apimachinery/pkg/labels"

victoriametricsv1beta1 "github.com/VictoriaMetrics/operator/api/v1beta1"
"github.com/VictoriaMetrics/operator/controllers/factory/k8stools"
"github.com/VictoriaMetrics/operator/controllers/factory/psp"
Expand Down Expand Up @@ -43,9 +45,7 @@ func CreateOrUpdateVMAlertService(ctx context.Context, cr *victoriametricsv1beta
return nil, fmt.Errorf("cannot get vmalert service: %w", err)
}
}
for annotation, value := range currentService.Annotations {
newService.Annotations[annotation] = value
}
newService.Annotations = labels.Merge(newService.Annotations, currentService.Annotations)
if currentService.Spec.ClusterIP != "" {
newService.Spec.ClusterIP = currentService.Spec.ClusterIP
}
Expand Down Expand Up @@ -136,13 +136,8 @@ func CreateOrUpdateVMAlert(ctx context.Context, cr *victoriametricsv1beta1.VMAle
return reconcile.Result{}, fmt.Errorf("cannot get deploy for vmalert: %w", err)
}
}
for annotation, value := range currDeploy.Annotations {
newDeploy.Annotations[annotation] = value
}
for annotation, value := range currDeploy.Spec.Template.Annotations {
newDeploy.Spec.Template.Annotations[annotation] = value
}

newDeploy.Annotations = labels.Merge(newDeploy.Annotations, currDeploy.Annotations)
newDeploy.Spec.Template.Annotations = labels.Merge(newDeploy.Spec.Template.Annotations, currDeploy.Spec.Template.Annotations)
err = rclient.Update(ctx, newDeploy)
if err != nil {
return reconcile.Result{}, fmt.Errorf("cannot update vmalert deploy: %w", err)
Expand Down
65 changes: 9 additions & 56 deletions controllers/factory/vmcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,8 @@ func createOrUpdateVMSelect(ctx context.Context, cr *v1beta1.VMCluster, rclient
}
}
l.Info("vmstorage was found, updating it")
if newSts.Annotations == nil {
newSts.Annotations = make(map[string]string)
}
for annotation, value := range currentSts.Annotations {
newSts.Annotations[annotation] = value
}

if newSts.Spec.Template.Annotations == nil {
newSts.Spec.Template.Annotations = make(map[string]string)
}
for annotation, value := range currentSts.Spec.Template.Annotations {
newSts.Spec.Template.Annotations[annotation] = value
}
newSts.Annotations = labels.Merge(newSts.Annotations, currentSts.Annotations)
newSts.Spec.Template.Annotations = labels.Merge(newSts.Spec.Template.Annotations, currentSts.Spec.Template.Annotations)
if currentSts.ManagedFields != nil {
newSts.ManagedFields = currentSts.ManagedFields
}
Expand Down Expand Up @@ -256,12 +245,7 @@ func CreateOrUpdateVMSelectService(ctx context.Context, cr *v1beta1.VMCluster, r
return nil, fmt.Errorf("cannot get vmselect service: %w", err)
}
}
if newService.Annotations == nil {
newService.Annotations = make(map[string]string)
}
for annotation, value := range currentService.Annotations {
newService.Annotations[annotation] = value
}
newService.Annotations = labels.Merge(newService.Annotations, currentService.Annotations)
if currentService.Spec.ClusterIP != "" {
newService.Spec.ClusterIP = currentService.Spec.ClusterIP
}
Expand Down Expand Up @@ -300,19 +284,8 @@ func createOrUpdateVMInsert(ctx context.Context, cr *v1beta1.VMCluster, rclient
}
l.Info("vminsert was found, updating it")

if newDeployment.Annotations == nil {
newDeployment.Annotations = make(map[string]string)
}
for annotation, value := range currentDeployment.Annotations {
newDeployment.Annotations[annotation] = value
}

if newDeployment.Spec.Template.Annotations == nil {
newDeployment.Spec.Template.Annotations = make(map[string]string)
}
for annotation, value := range currentDeployment.Spec.Template.Annotations {
newDeployment.Spec.Template.Annotations[annotation] = value
}
newDeployment.Annotations = labels.Merge(newDeployment.Annotations, currentDeployment.Annotations)
newDeployment.Spec.Template.Annotations = labels.Merge(newDeployment.Spec.Template.Annotations, currentDeployment.Spec.Template.Annotations)

err = rclient.Update(ctx, newDeployment)
if err != nil {
Expand Down Expand Up @@ -340,13 +313,8 @@ func CreateOrUpdateVMInsertService(ctx context.Context, cr *v1beta1.VMCluster, r
return nil, fmt.Errorf("cannot get vminsert service: %w", err)
}
}
newService.Annotations = labels.Merge(newService.Annotations, currentService.Annotations)

if newService.Annotations == nil {
newService.Annotations = make(map[string]string)
}
for annotation, value := range currentService.Annotations {
newService.Annotations[annotation] = value
}
if currentService.Spec.ClusterIP != "" {
newService.Spec.ClusterIP = currentService.Spec.ClusterIP
}
Expand Down Expand Up @@ -385,19 +353,9 @@ func createOrUpdateVMStorage(ctx context.Context, cr *v1beta1.VMCluster, rclient
}
}
l.Info("vmstorage was found, updating it")
if newSts.Annotations == nil {
newSts.Annotations = make(map[string]string)
}
for annotation, value := range currentSts.Annotations {
newSts.Annotations[annotation] = value
}
newSts.Annotations = labels.Merge(newSts.Annotations, currentSts.Annotations)
newSts.Spec.Template.Annotations = labels.Merge(newSts.Spec.Template.Annotations, currentSts.Spec.Template.Annotations)

if newSts.Spec.Template.Annotations == nil {
newSts.Spec.Template.Annotations = make(map[string]string)
}
for annotation, value := range currentSts.Spec.Template.Annotations {
newSts.Spec.Template.Annotations[annotation] = value
}
// hack for break reconcile loop at kubernetes 1.18
newSts.Status.Replicas = currentSts.Status.Replicas
err = rclient.Update(ctx, newSts)
Expand Down Expand Up @@ -426,12 +384,7 @@ func CreateOrUpdateVMStorageService(ctx context.Context, cr *v1beta1.VMCluster,
return nil, fmt.Errorf("cannot get vmstorage service: %w", err)
}
}
if newService.Annotations == nil {
newService.Annotations = make(map[string]string)
}
for annotation, value := range currentService.Annotations {
newService.Annotations[annotation] = value
}
newService.Annotations = labels.Merge(newService.Annotations, currentService.Annotations)
if currentService.Spec.ClusterIP != "" {
newService.Spec.ClusterIP = currentService.Spec.ClusterIP
}
Expand Down
Loading

0 comments on commit 771605e

Please sign in to comment.