Skip to content

Commit

Permalink
liqoctl: upgrade CRDs on chart upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
aleoli authored and adamjensenbot committed May 31, 2022
1 parent cebc5b2 commit d11c3e0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ require (
sigs.k8s.io/aws-iam-authenticator v0.5.7
sigs.k8s.io/controller-runtime v0.11.2
sigs.k8s.io/sig-storage-lib-external-provisioner/v7 v7.0.1
sigs.k8s.io/yaml v1.3.0
)

require (
Expand Down Expand Up @@ -232,7 +233,6 @@ require (
sigs.k8s.io/kustomize/api v0.11.4 // indirect
sigs.k8s.io/kustomize/kyaml v0.13.6 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace github.com/grandcat/zeroconf => github.com/liqotech/zeroconf v1.0.1-0.20201020081245-6384f3f21ffb
Expand Down
30 changes: 30 additions & 0 deletions pkg/liqoctl/install/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ import (
"helm.sh/helm/pkg/strvals"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/repo"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
k8syaml "sigs.k8s.io/yaml"

"github.com/liqotech/liqo/pkg/consts"
"github.com/liqotech/liqo/pkg/liqoctl/factory"
Expand Down Expand Up @@ -241,6 +244,33 @@ func (o *Options) installOrUpdate(ctx context.Context, rawValues string) error {
Wait: true,
}

// install or update CRDs
chart, _, err := o.HelmClient().GetChart(o.ChartPath, &action.ChartPathOptions{Version: o.Version})
if err != nil {
return fmt.Errorf("unable to get the helm chart: %w", err)
}
crds := chart.CRDObjects()
for i := range crds {
crdObj := apiextensionsv1.CustomResourceDefinition{}
if err = k8syaml.Unmarshal(crds[i].File.Data, &crdObj); err != nil {
return fmt.Errorf("unable to unmarshal CRD yaml file %q: %w", crds[i].File.Name, err)
}
err = o.CRClient.Create(ctx, &crdObj)
switch {
case apierrors.IsAlreadyExists(err):
var existingCrd apiextensionsv1.CustomResourceDefinition
if err = o.CRClient.Get(ctx, client.ObjectKeyFromObject(&crdObj), &existingCrd); err != nil {
return fmt.Errorf("unable to get CRD %q: %w", crdObj.Name, err)
}
existingCrd.Spec = *crdObj.Spec.DeepCopy()
if err = o.CRClient.Update(ctx, &existingCrd); err != nil {
return fmt.Errorf("unable to update CRD %q: %w", crdObj.Name, err)
}
case err != nil:
return fmt.Errorf("unable to create CRD %q: %w", crdObj.Name, err)
}
}

// provide the possibility to exit installation on context cancellation
errCh := make(chan error)
defer close(errCh)
Expand Down

0 comments on commit d11c3e0

Please sign in to comment.