Skip to content

Commit

Permalink
feat: automatically update componentconfig tutorial
Browse files Browse the repository at this point in the history
Co-authored-by: Camila Macedo <7708031+camilamacedo86@users.noreply.github.com>

chore: refactor
  • Loading branch information
Eileen-Yu committed Feb 1, 2023
1 parent fc673e4 commit 2e70b94
Show file tree
Hide file tree
Showing 11 changed files with 248 additions and 70 deletions.
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

# Copyright 2019 The Kubernetes Authors.
# 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.
Expand Down Expand Up @@ -63,14 +63,18 @@ install: build ## Build and install the binary with the current source code. Use
##@ Development

.PHONY: generate
generate: generate-testdata ## Update/generate all mock data. You should run this commands to update the mock data after your changes.
generate: generate-testdata generate-docs ## Update/generate all mock data. You should run this commands to update the mock data after your changes.
go mod tidy

.PHONY: generate-testdata
generate-testdata: ## Update/generate the testdata in $GOPATH/src/sigs.k8s.io/kubebuilder
rm -rf testdata/
./test/testdata/generate.sh

.PHONY: generate-docs
generate-docs: ## Update/generate the docs in $GOPATH/src/sigs.k8s.io/kubebuilder
go run hack/docs/generate_samples.go

.PHONY: lint
lint: golangci-lint yamllint ## Run golangci-lint linter & yamllint
$(GOLANGCI_LINT) run
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ More information can be found via the [Kubebuilder Documentation](https://book.k

## License

Copyright 2023.
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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023.
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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023.
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.
Expand Down Expand Up @@ -49,7 +49,6 @@ type ProjectConfig struct {

Spec ProjectConfigSpec `json:"spec,omitempty"`
Status ProjectConfigStatus `json:"status,omitempty"`

// ControllerManagerConfigurationSpec returns the configurations for controllers
cfg.ControllerManagerConfigurationSpec `json:",inline"`

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023.
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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023.
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.
Expand Down
22 changes: 22 additions & 0 deletions docs/docs.sh
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

49 changes: 49 additions & 0 deletions hack/docs/generate_samples.go
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()
}
165 changes: 165 additions & 0 deletions hack/docs/internal/generate_component_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
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)

// 2. 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"`+"`",
`
// ControllerManagerConfigurationSpec returns the configurations for controllers
cfg.ControllerManagerConfigurationSpec `+"`"+`json:",inline"`+"`"+`
ClusterName string `+"`"+`json:"clusterName,omitempty"`+"`",
)

CheckError("fixing projectconfig_types", err)

// 3. fix main
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "main.go"),
`var err error`,
`
ctrlConfig := configv2.ProjectConfig{}`)
CheckError("fixing main.go", err)

err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "main.go"),
`AtPath(configFile)`,
`.OfKind(&ctrlConfig)`)
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)
}
}

0 comments on commit 2e70b94

Please sign in to comment.