-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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 <[email protected]> chore: refactor
- Loading branch information
Showing
11 changed files
with
261 additions
and
70 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
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 |
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
2 changes: 1 addition & 1 deletion
2
docs/book/src/component-config-tutorial/testdata/project/main.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
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,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) | ||
} | ||
} |