|
| 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 | +package hybrid |
| 16 | + |
| 17 | +import ( |
| 18 | + "os" |
| 19 | + "path/filepath" |
| 20 | + |
| 21 | + log "github.com/sirupsen/logrus" |
| 22 | + |
| 23 | + "github.com/operator-framework/helm-operator-plugins/hack/generate/samples/internal/pkg" |
| 24 | +) |
| 25 | + |
| 26 | +// Memcached defines the Memcached Sample in Helm |
| 27 | +type Memcached struct { |
| 28 | + ctx *pkg.SampleContext |
| 29 | +} |
| 30 | + |
| 31 | +// GenerateMemcachedSample will call all actions to create the directory and generate the sample |
| 32 | +// The Context to run the samples are not the same in the e2e test. In this way, note that it should NOT |
| 33 | +// be called in the e2e tests since it will call the Prepare() to set the sample context and generate the files |
| 34 | +// in the testdata directory. The e2e tests only ought to use the Run() method with the TestContext. |
| 35 | +func GenerateMemcachedSample(binaryPath, samplesPath string) { |
| 36 | + ctx, err := pkg.NewSampleContext(binaryPath, filepath.Join(samplesPath, "memcached-operator"), |
| 37 | + "GO111MODULE=on") |
| 38 | + pkg.CheckError("generating Helm memcached context", err) |
| 39 | + |
| 40 | + memcached := Memcached{&ctx} |
| 41 | + memcached.Prepare() |
| 42 | + memcached.Run() |
| 43 | +} |
| 44 | + |
| 45 | +// Prepare the Context for the Memcached Helm Sample |
| 46 | +// Note that sample directory will be re-created and the context data for the sample |
| 47 | +// will be set such as the domain and GVK. |
| 48 | +func (mh *Memcached) Prepare() { |
| 49 | + log.Infof("destroying directory for memcached helm samples") |
| 50 | + mh.ctx.Destroy() |
| 51 | + |
| 52 | + log.Infof("creating directory") |
| 53 | + err := mh.ctx.Prepare() |
| 54 | + pkg.CheckError("creating directory", err) |
| 55 | + |
| 56 | + log.Infof("setting domain and GVK") |
| 57 | + mh.ctx.Version = "v1alpha1" |
| 58 | + mh.ctx.Group = "cache" |
| 59 | + mh.ctx.Kind = "Memcached" |
| 60 | +} |
| 61 | + |
| 62 | +func (mh *Memcached) Run() { |
| 63 | + // When we scaffold Helm based projects, it tries to use the discovery API of a Kubernetes |
| 64 | + // cluster to intelligently build the RBAC rules that the operator will require based on the |
| 65 | + // content of the helm chart. |
| 66 | + // |
| 67 | + // Here, we intentionally set KUBECONFIG to a broken value to ensure that operator-sdk will be |
| 68 | + // unable to reach a real cluster, and thus will generate a default RBAC rule set. This is |
| 69 | + // required to make Helm project generation idempotent because contributors and CI environments |
| 70 | + // can all have slightly different environments that can affect the content of the generated |
| 71 | + // role and cause sanity testing to fail. |
| 72 | + os.Setenv("KUBECONFIG", "broken_so_we_generate_static_default_rules") |
| 73 | + log.Infof("using init command and scaffolding the project") |
| 74 | + |
| 75 | + err := mh.ctx.Init( |
| 76 | + "--plugins", "hybrid/v1-alpha", |
| 77 | + "--repo", "github.com/example/memcached-operator", |
| 78 | + ) |
| 79 | + |
| 80 | + pkg.CheckError("creating the project", err) |
| 81 | + |
| 82 | + // TODO: Uncomment this code after helm plugin is migrated |
| 83 | + // err = mh.ctx.CreateAPI( |
| 84 | + // "--plugins", "helm.sdk.operatorframework.io/v1", |
| 85 | + // "--group", mh.ctx.Group, |
| 86 | + // "--version", mh.ctx.Version, |
| 87 | + // "--kind", mh.ctx.Kind, |
| 88 | + // ) |
| 89 | + |
| 90 | + // pkg.CheckError("creating helm api", err) |
| 91 | + |
| 92 | + err = mh.ctx.CreateAPI( |
| 93 | + "--plugins", "base.go.kubebuilder.io/v3", |
| 94 | + "--group", mh.ctx.Group, |
| 95 | + "--version", mh.ctx.Version, |
| 96 | + "--kind", mh.ctx.Kind, |
| 97 | + "--resource", "--controller", |
| 98 | + ) |
| 99 | + |
| 100 | + pkg.CheckError("creating go api", err) |
| 101 | +} |
0 commit comments