|
| 1 | +// Copyright 2020 The Operator-SDK Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// TODO: This implementation is already done for v3+. Also, it might be |
| 16 | +// addressed on v2 as well. More info: https://github.com/kubernetes-sigs/kubebuilder/pull/1711 |
| 17 | +package envtest |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io/ioutil" |
| 22 | + "os" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "sigs.k8s.io/kubebuilder/pkg/model/config" |
| 26 | +) |
| 27 | + |
| 28 | +// controllerRuntimeVersion version to be used to download the envtest setup script |
| 29 | +const controllerRuntimeVersion = "v0.6.3" |
| 30 | + |
| 31 | +// RunInit modifies the project scaffolded by kubebuilder's Init plugin. |
| 32 | +func RunInit(cfg *config.Config) error { |
| 33 | + // Only run these if project version is v3. |
| 34 | + if !cfg.IsV3() { |
| 35 | + return nil |
| 36 | + } |
| 37 | + |
| 38 | + // Update the scaffolded Makefile with operator-sdk recipes. |
| 39 | + if err := initUpdateMakefile("Makefile"); err != nil { |
| 40 | + return fmt.Errorf("error updating Makefile: %v", err) |
| 41 | + } |
| 42 | + return nil |
| 43 | +} |
| 44 | + |
| 45 | +// initUpdateMakefile updates a vanilla kubebuilder Makefile with operator-sdk recipes. |
| 46 | +func initUpdateMakefile(filePath string) error { |
| 47 | + makefileBytes, err := ioutil.ReadFile(filePath) |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + |
| 52 | + makefileBytes = []byte(strings.Replace(string(makefileBytes), |
| 53 | + "# Run tests\ntest: generate fmt vet manifests\n\tgo test ./... -coverprofile cover.out", |
| 54 | + fmt.Sprintf(makefileTestTarget, controllerRuntimeVersion), 1)) |
| 55 | + |
| 56 | + var mode os.FileMode = 0644 |
| 57 | + if info, err := os.Stat(filePath); err != nil { |
| 58 | + mode = info.Mode() |
| 59 | + } |
| 60 | + return ioutil.WriteFile(filePath, makefileBytes, mode) |
| 61 | +} |
| 62 | + |
| 63 | +const makefileTestTarget = `# Run tests |
| 64 | +ENVTEST_ASSETS_DIR = $(shell pwd)/testbin |
| 65 | +test: generate fmt vet manifests |
| 66 | + mkdir -p $(ENVTEST_ASSETS_DIR) |
| 67 | + test -f $(ENVTEST_ASSETS_DIR)/setup-envtest.sh || curl -sSLo $(ENVTEST_ASSETS_DIR)/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/%s/hack/setup-envtest.sh |
| 68 | + source $(ENVTEST_ASSETS_DIR)/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out` |
0 commit comments