diff --git a/cmd/preupgradechecks/main.go b/cmd/preupgradechecks/main.go index d62e827b..0c7fa7e3 100644 --- a/cmd/preupgradechecks/main.go +++ b/cmd/preupgradechecks/main.go @@ -65,11 +65,15 @@ Options: zap.Error(err)) } - installed := crdBackedClient.IsFissionReInstall() - if !installed { + crd := crdBackedClient.GetFunctionCRD() + if crd == nil { logger.Info("nothing to do since CRDs are not present on the cluster") return } + err = crdBackedClient.LatestSchemaApplied() + if err != nil { + logger.Fatal("New CRDs are not applied") + } crdBackedClient.VerifyFunctionSpecReferences() } diff --git a/cmd/preupgradechecks/preupgradechecks.go b/cmd/preupgradechecks/preupgradechecks.go index c5f82c5a..3359a960 100644 --- a/cmd/preupgradechecks/preupgradechecks.go +++ b/cmd/preupgradechecks/preupgradechecks.go @@ -18,10 +18,12 @@ package main import ( "fmt" + "strings" multierror "github.com/hashicorp/go-multierror" "github.com/pkg/errors" "go.uber.org/zap" + "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" k8serrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -63,18 +65,32 @@ func makePreUpgradeTaskClient(logger *zap.Logger, fnPodNs, envBuilderNs string) }, nil } -// IsFissionReInstall checks if there is at least one fission CRD, i.e. function in this case, on this cluster. -// We need this to find out if fission had been previously installed on this cluster -func (client *PreUpgradeTaskClient) IsFissionReInstall() bool { +// GetFunctionCRD checks if function CRD is present on the cluster and returns it. It returns nil if not found +// We can use this to find out if fission had been previously installed on this cluster too. +func (client *PreUpgradeTaskClient) GetFunctionCRD() *v1beta1.CustomResourceDefinition { for i := 0; i < maxRetries; i++ { - _, err := client.apiExtClient.ApiextensionsV1beta1().CustomResourceDefinitions().Get(FunctionCRD, metav1.GetOptions{}) + crd, err := client.apiExtClient.ApiextensionsV1beta1().CustomResourceDefinitions().Get(FunctionCRD, metav1.GetOptions{}) if err != nil && k8serrors.IsNotFound(err) { - return false - + continue } + return crd } + return nil +} - return false +// LatestSchemaApplied ensures that the end user has applied the latest CRDs generated to the cluster. +// For future reference: whenever a new field is added, we need to check for that field's existence in this function +func (client *PreUpgradeTaskClient) LatestSchemaApplied() error { + client.logger.Info("Checking if user has applied the latest CRDs") + crd := client.GetFunctionCRD() + if crd == nil { + return errors.New("Could not get the Function CRD") + } + // Any new field added in Function spec can be checked here provided the substring matches the description in CRD Validation of the field + if !strings.Contains(crd.Spec.String(), "RequestsPerPod") { + return errors.New("Apply the newer CRDs before upgrading") + } + return nil } // VerifyFunctionSpecReferences verifies that a function references secrets, configmaps, pkgs in its own namespace and