-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: automatically update componentconfig tutorial
Co-authored-by: Camila Macedo <7708031+camilamacedo86@users.noreply.github.com>
- Loading branch information
1 parent
fc673e4
commit fb8379c
Showing
11 changed files
with
252 additions
and
72 deletions.
There are no files selected for viewing
This file contains 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
61 changes: 0 additions & 61 deletions
61
docs/book/src/component-config-tutorial/testdata/generate_componentconfig.sh
This file was deleted.
Oops, something went wrong.
This file contains 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
2 changes: 1 addition & 1 deletion
2
docs/book/src/component-config-tutorial/testdata/project/api/v2/groupversion_info.go
This file contains 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 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
2 changes: 1 addition & 1 deletion
2
docs/book/src/component-config-tutorial/testdata/project/api/v2/zz_generated.deepcopy.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
docs/book/src/component-config-tutorial/testdata/project/hack/boilerplate.go.txt
This file contains 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 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 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,22 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Copyright 2023 The Kubernetes 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. | ||
|
||
go run docs/book/src/update-docs/generate_docs.go | ||
|
||
cd docs/book/src/component-config-tutorial/testdata/project | ||
|
||
make build | ||
|
This file contains 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,49 @@ | ||
/* | ||
Copyright 2023 The Kubernetes 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 main | ||
|
||
import ( | ||
"fmt" | ||
|
||
componentconfig "sigs.k8s.io/kubebuilder/v3/hack/docs/internal" | ||
) | ||
|
||
func main() { | ||
fmt.Println("Generating documents...") | ||
|
||
fmt.Println("Generating component-config tutorial...") | ||
UpdateComponentConfigTutorial() | ||
|
||
// TODO: Generate cronjob-tutorial | ||
|
||
// TODO: Generate multiversion-tutorial | ||
} | ||
|
||
func UpdateComponentConfigTutorial() { | ||
binaryPath := "/tmp/kubebuilder/bin/kubebuilder" | ||
samplePath := "docs/book/src/component-config-tutorial/testdata/project/" | ||
|
||
sp := componentconfig.NewSample(binaryPath, samplePath) | ||
|
||
sp.Prepare() | ||
|
||
sp.GenerateSampleProject() | ||
|
||
sp.UpdateTutorial() | ||
|
||
sp.CodeGen() | ||
} |
This file contains 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,168 @@ | ||
/* | ||
Copyright 2023 The Kubernetes 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 componentconfig | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
|
||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/spf13/afero" | ||
pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util" | ||
"sigs.k8s.io/kubebuilder/v3/test/e2e/utils" | ||
) | ||
|
||
type Sample struct { | ||
ctx *utils.TestContext | ||
} | ||
|
||
func NewSample(binaryPath, samplePath string) Sample { | ||
log.Infof("Generating the sample context of component-config...") | ||
|
||
ctx := newSampleContext(binaryPath, samplePath, "GO111MODULE=on") | ||
|
||
return Sample{&ctx} | ||
} | ||
|
||
func newSampleContext(binaryPath string, samplePath string, env ...string) utils.TestContext { | ||
cmdContext := &utils.CmdContext{ | ||
Env: env, | ||
Dir: samplePath, | ||
} | ||
|
||
testContext := utils.TestContext{ | ||
CmdContext: cmdContext, | ||
BinaryName: binaryPath, | ||
} | ||
|
||
return testContext | ||
} | ||
|
||
// Prepare the Context for the sample project | ||
func (sp *Sample) Prepare() { | ||
log.Infof("destroying directory for sample project") | ||
sp.ctx.Destroy() | ||
|
||
log.Infof("refreshing tools and creating directory...") | ||
err := sp.ctx.Prepare() | ||
|
||
CheckError("creating directory for sample project", err) | ||
} | ||
|
||
func (sp *Sample) GenerateSampleProject() { | ||
log.Infof("Initializing the project") | ||
err := sp.ctx.Init( | ||
"--domain", "tutorial.kubebuilder.io", | ||
"--repo", "tutorial.kubebuilder.io/project", | ||
"--license", "apache2", | ||
"--owner", "The Kubernetes authors", | ||
"--component-config", | ||
) | ||
CheckError("Initializing the project", err) | ||
|
||
log.Infof("Adding a new config type") | ||
err = sp.ctx.CreateAPI( | ||
"--group", "config", | ||
"--version", "v2", | ||
"--kind", "ProjectConfig", | ||
"--resource", "--controller=false", | ||
"--make=false", | ||
) | ||
CheckError("Creating the API", err) | ||
} | ||
|
||
func (sp *Sample) UpdateTutorial() { | ||
// 1. generate controller_manager_config.yaml | ||
var fs = afero.NewOsFs() | ||
err := afero.WriteFile(fs, filepath.Join(sp.ctx.Dir, "config/manager/controller_manager_config.yaml"), | ||
[]byte(`apiVersion: controller-runtime.sigs.k8s.io/v1alpha1 | ||
kind: ControllerManagerConfig | ||
metadata: | ||
labels: | ||
app.kubernetes.io/name: controllermanagerconfig | ||
app.kubernetes.io/instance: controller-manager-configuration | ||
app.kubernetes.io/component: manager | ||
app.kubernetes.io/created-by: project | ||
app.kubernetes.io/part-of: project | ||
app.kubernetes.io/managed-by: kustomize | ||
health: | ||
healthProbeBindAddress: :8081 | ||
metrics: | ||
bindAddress: 127.0.0.1:8080 | ||
webhook: | ||
port: 9443 | ||
leaderElection: | ||
leaderElect: true | ||
resourceName: 80807133.tutorial.kubebuilder.io | ||
clusterName: example-test | ||
`), 0600) | ||
CheckError("fixing controller_manager_config", err) | ||
|
||
// 3. fix projectconfig_types.go | ||
err = pluginutil.InsertCode( | ||
filepath.Join(sp.ctx.Dir, "api/v2/projectconfig_types.go"), | ||
`metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"`, | ||
` | ||
cfg "sigs.k8s.io/controller-runtime/pkg/config/v1alpha1"`,) | ||
CheckError("fixing projectconfig_types", err) | ||
|
||
err = pluginutil.InsertCode( | ||
filepath.Join(sp.ctx.Dir, "api/v2/projectconfig_types.go"), | ||
`Status ProjectConfigStatus ` + "`" + `json:"status,omitempty"` + "`", | ||
//nolint:lll | ||
` | ||
// ControllerManagerConfigurationSpec returns the configurations for controllers | ||
cfg.ControllerManagerConfigurationSpec ` + "`" + `json:",inline"` + "`" + ` | ||
ClusterName string ` + "`" + `json:"clusterName,omitempty"`+ "`",) | ||
CheckError("fixing projectconfig_types", err) | ||
|
||
// TODO: Adjust main based on tutorial | ||
// 6. fix main | ||
/* | ||
err = pluginutil.ReplaceInFile( | ||
filepath.Join(sp.ctx.Dir, "main.go"), | ||
//nolint:lll | ||
`flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") | ||
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") | ||
flag.BoolVar(&enableLeaderElection, "leader-elect", false, | ||
"Enable leader election for controller manager. "+ | ||
"Enabling this will ensure there is only one active controller manager.") | ||
`, | ||
//nolint:lll | ||
"var configFile string\n flag.StringVar(&configFile, \"config\", \"\",\n \"The controller will load its initial configuration from this file. \"+\n \"Omit this flag to use the default configuration values. \"+\n \"Command-line flags override configuration from this file.\")\n", | ||
) | ||
CheckError("fixing main.go", err) | ||
*/ | ||
} | ||
|
||
func (sp *Sample) CodeGen() { | ||
cmd := exec.Command("make", "build") | ||
_, err := sp.ctx.Run(cmd) | ||
|
||
CheckError("Failed to generate code in componentconfig tutorial", err) | ||
} | ||
|
||
// CheckError will exit with exit code 1 when err is not nil. | ||
func CheckError(msg string, err error) { | ||
if err != nil { | ||
log.Errorf("error %s: %s", msg, err) | ||
os.Exit(1) | ||
} | ||
} |