-
Notifications
You must be signed in to change notification settings - Fork 1.8k
cleanup: keeping sdk scaffolds centralized #3349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
camilamacedo86
merged 2 commits into
operator-framework:master
from
camilamacedo86:just-go-cleanup
Jul 13, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| // Copyright 2020 The Operator-SDK 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. | ||
|
|
||
| // TODO: rewrite this when plugins phase 2 is implemented. | ||
| package plugins | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io/ioutil" | ||
|
|
||
| "sigs.k8s.io/kubebuilder/pkg/model/config" | ||
|
|
||
| "github.com/operator-framework/operator-sdk/internal/util/projutil" | ||
| ) | ||
|
|
||
| // UpdateMakefile modifies the project scaffolded by kubebuilder's Init plugin. | ||
| func UpdateMakefile(cfg *config.Config) error { | ||
| // Update the scaffolded Makefile with operator-sdk recipes. | ||
| if err := initUpdateMakefile("Makefile", cfg); err != nil { | ||
| return fmt.Errorf("error updating Makefile: %v", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // initUpdateMakefile updates a vanilla kubebuilder Makefile with operator-sdk recipes. | ||
| func initUpdateMakefile(filePath string, cfg *config.Config) error { | ||
| makefileBytes, err := ioutil.ReadFile(filePath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Prepend bundle variables. | ||
| makefileBytes = append([]byte(makefileBundleVarFragment), makefileBytes...) | ||
|
|
||
| // Append bundle recipes. | ||
| operatorType := projutil.PluginKeyToOperatorType(cfg.Layout) | ||
| switch operatorType { | ||
| case projutil.OperatorTypeUnknown: | ||
| return fmt.Errorf("unsupported plugin key %q", cfg.Layout) | ||
| case projutil.OperatorTypeGo: | ||
| makefileBytes = append(makefileBytes, []byte(makefileBundleFragmentGo)...) | ||
| default: | ||
| makefileBytes = append(makefileBytes, []byte(makefileBundleFragmentNonGo)...) | ||
| } | ||
|
|
||
| makefileBytes = append(makefileBytes, []byte(makefileBundleBuildFragment)...) | ||
|
|
||
| return ioutil.WriteFile(filePath, makefileBytes, 0644) | ||
| } | ||
|
|
||
| // Makefile fragments to add to the base Makefile. | ||
| const ( | ||
| makefileBundleVarFragment = `# Current Operator version | ||
| VERSION ?= 0.0.1 | ||
| # Default bundle image tag | ||
| BUNDLE_IMG ?= controller-bundle:$(VERSION) | ||
| # Options for 'bundle-build' | ||
| ifneq ($(origin CHANNELS), undefined) | ||
| BUNDLE_CHANNELS := --channels=$(CHANNELS) | ||
| endif | ||
| ifneq ($(origin DEFAULT_CHANNEL), undefined) | ||
| BUNDLE_DEFAULT_CHANNEL := --default-channel=$(DEFAULT_CHANNEL) | ||
| endif | ||
| BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL) | ||
|
|
||
| .PHONY: bundle | ||
| ` | ||
|
|
||
| makefileBundleFragmentGo = ` | ||
| # Generate bundle manifests and metadata, then validate generated files. | ||
| bundle: manifests | ||
| operator-sdk generate kustomize manifests -q | ||
| $(KUSTOMIZE) build config/manifests | operator-sdk generate bundle -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS) | ||
| operator-sdk bundle validate ./bundle | ||
| ` | ||
|
|
||
| makefileBundleFragmentNonGo = ` | ||
| # Generate bundle manifests and metadata, then validate generated files. | ||
| bundle: kustomize | ||
| operator-sdk generate kustomize manifests -q | ||
| $(KUSTOMIZE) build config/manifests | operator-sdk generate bundle -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS) | ||
| operator-sdk bundle validate ./bundle | ||
| ` | ||
|
|
||
| makefileBundleBuildFragment = ` | ||
| # Build the bundle image. | ||
| bundle-build: | ||
| docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) . | ||
| ` | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // Copyright 2020 The Operator-SDK 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. | ||
|
|
||
| // TODO: rewrite this when plugins phase 2 is implemented. | ||
| package plugins | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "sigs.k8s.io/kubebuilder/pkg/model/config" | ||
|
|
||
| "github.com/operator-framework/operator-sdk/internal/scaffold/kustomize" | ||
| ) | ||
|
|
||
| // sampleKustomizationFragment is a template for samples/kustomization.yaml. | ||
| const sampleKustomizationFragment = `## This file is auto-generated, do not modify ## | ||
| resources: | ||
| ` | ||
|
|
||
| // WriteSamplesKustomization perform the SDK plugin-specific scaffolds. | ||
| func WriteSamplesKustomization(cfg *config.Config) error { | ||
|
|
||
| // Write CR paths to the samples' kustomization file. This file has a | ||
| // "do not modify" comment so it is safe to overwrite. | ||
| samplesKustomization := sampleKustomizationFragment | ||
| for _, gvk := range cfg.Resources { | ||
| samplesKustomization += fmt.Sprintf("- %s\n", makeCRFileName(gvk)) | ||
| } | ||
| kpath := filepath.Join("config", "samples") | ||
| if err := kustomize.Write(kpath, samplesKustomization); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // todo(camilamacedo86): Now that we have the Kubebuilder scaffolding machinery included in our repo, we could make | ||
| // this an actual template that supports both file.Template and file.Inserter for init and create api, respectively. | ||
| // More info: https://github.com/operator-framework/operator-sdk/issues/3370 | ||
| // makeCRFileName returns a Custom Resource example file name in the same format | ||
| // as kubebuilder's CreateAPI plugin for a gvk. | ||
| func makeCRFileName(gvk config.GVK) string { | ||
| return fmt.Sprintf("%s_%s_%s.yaml", gvk.Group, gvk.Version, strings.ToLower(gvk.Kind)) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Call
p.run()here instead of at the end.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shows no make difference, however, done. 👍