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 3, 2023
1 parent fc673e4 commit 0be2b9a
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 69 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Docs verification

# Trigger the workflow on pull requests and direct pushes to any branch
on:
push:
pull_request:

jobs:
testdata:
name: Verify Docs update
runs-on: ubuntu-latest
# Pull requests from the same repository won't trigger this checks as they were already triggered by the push
if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository
steps:
- name: Clone the code
uses: actions/checkout@v3
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: "1.19"
- name: Remove pre-installed kustomize
# This step is needed as the following one tries to remove
# kustomize for each test but has no permission to do so
run: sudo rm -f /usr/local/bin/kustomize
- name: Verify docs
run: |
make generate-docs
if [[ $(git status --porcelain) ]]; then
git diff
echo "Generate Docs failed!"
echo "Please, if you have changed the scaffolding make sure you have run: make generate-docs"
exit 1
else
echo "Generate Docs passed!"
fi
6 changes: 5 additions & 1 deletion 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 @@ -71,6 +71,10 @@ generate-testdata: ## Update/generate the testdata in $GOPATH/src/sigs.k8s.io/ku
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
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 0be2b9a

Please sign in to comment.