Skip to content

Commit

Permalink
feat(install, crd): add env to enable/disable CRD installation
Browse files Browse the repository at this point in the history
add OPENEBS_IO_CRD_INSTALL environment variable to enable / disable
installation of BlockDevice and BlockDeviceClaim CRDs

Signed-off-by: Akhil Mohan <[email protected]>
  • Loading branch information
akhilerm authored and kmova committed May 12, 2020
1 parent f59ce1d commit 63ca87a
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 12 deletions.
1 change: 1 addition & 0 deletions cmd/421_akhilerm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add env to enable / disable CRD installation
29 changes: 17 additions & 12 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"flag"
"fmt"
"github.com/openebs/node-disk-manager/pkg/env"
"github.com/openebs/node-disk-manager/pkg/setup"
"github.com/openebs/node-disk-manager/pkg/upgrade"
"github.com/openebs/node-disk-manager/pkg/upgrade/v040_041"
Expand Down Expand Up @@ -100,18 +101,22 @@ func main() {
os.Exit(1)
}

log.Info("Installing the components")
// get a new install setup
setupConfig, err := setup.NewInstallSetup(cfg)
if err != nil {
log.Error(err, "")
os.Exit(1)
}

// install the components
if err = setupConfig.Install(); err != nil {
log.Error(err, "")
os.Exit(1)
// check if CRDs need to be installed.
// The OPENEBS_IO_INSTALL_CRD env is checked
if env.IsInstallCRDEnabled() {
log.Info("Installing the components")
// get a new install setup
setupConfig, err := setup.NewInstallSetup(cfg)
if err != nil {
log.Error(err, "")
os.Exit(1)
}

// install the components
if err = setupConfig.Install(); err != nil {
log.Error(err, "")
os.Exit(1)
}
}

log.Info("Registering Components")
Expand Down
44 changes: 44 additions & 0 deletions pkg/env/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2020 The OpenEBS Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package env

import (
"os"

"github.com/openebs/node-disk-manager/pkg/util"
)

const (
// INSTALL_CRD_ENV is the environment variable used to check if
// CRDs need to be installed by NDM or not.
INSTALL_CRD_ENV = "OPENEBS_IO_INSTALL_CRD"

// installCRDEnvDefaultValue is the default value for the INSTALL_CRD_ENV
installCRDEnvDefaultValue = true
)

// IsInstallCRDEnabled is used to check whether the CRDs need to be installed
func IsInstallCRDEnabled() bool {
val := os.Getenv(INSTALL_CRD_ENV)

// if empty return the default value
if len(val) == 0 {
return installCRDEnvDefaultValue
}

return util.CheckTruthy(val)
}
60 changes: 60 additions & 0 deletions pkg/env/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright 2020 The OpenEBS Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package env

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestIsInstallCRDEnabled(t *testing.T) {
tests := map[string]struct {
setEnv bool
envValue string
want bool
}{
"when INSTALL_CRD_ENV is set to true": {
setEnv: true,
envValue: "true",
want: true,
},
"when INSTALL_CRD_ENV is set to false": {
setEnv: true,
envValue: "false",
},
"when INSTALL_CRD_ENV is not set": {
setEnv: false,
want: true,
},
"when INSTALL_CRD is set to empty": {
setEnv: true,
envValue: "",
want: true,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if tt.setEnv {
os.Setenv(INSTALL_CRD_ENV, tt.envValue)
}
assert.Equal(t, tt.want, IsInstallCRDEnabled())
_ = os.Unsetenv(INSTALL_CRD_ENV)
})
}
}

0 comments on commit 63ca87a

Please sign in to comment.