Skip to content

Commit

Permalink
switch module name & update config from external plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Eileen-Yu committed Aug 11, 2023
1 parent 18627f0 commit caac709
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"io"
"os"

"v1/external"
"v1/scaffolds"

"sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
// "sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
)

// Run will run the actual steps of the plugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ limitations under the License.
package cmd

import (
"v1/external"
"v1/scaffolds"

"github.com/spf13/pflag"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
// "sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
)

// flagsCmd handles all the logic for the `flags` subcommand of the sample external plugin.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ limitations under the License.
package cmd

import (
"v1/external"
"v1/scaffolds"

"github.com/spf13/pflag"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
// "sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
)

// metadataCmd handles all the logic for the `metadata` subcommand of the sample external plugin.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
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 external

import "sigs.k8s.io/kubebuilder/v3/pkg/plugin"

// PluginRequest contains all information kubebuilder received from the CLI
// and plugins executed before it.
type PluginRequest struct {
// APIVersion defines the versioned schema of PluginRequest that is being sent from Kubebuilder.
// Initially, this will be marked as alpha (v1alpha1).
APIVersion string `json:"apiVersion"`

// Args holds the plugin specific arguments that are received from the CLI
// which are to be passed down to the external plugin.
Args []string `json:"args"`

// Command contains the command to be executed by the plugin such as init, create api, etc.
Command string `json:"command"`

// Universe represents the modified file contents that gets updated over a series of plugin runs
// across the plugin chain. Initially, it starts out as empty.
Universe map[string]string `json:"universe"`

// Config stores the project configuration file.
Config string `json:"config"`
}

// PluginResponse is returned to kubebuilder by the plugin and contains all files
// written by the plugin following a certain command.
type PluginResponse struct {
// APIVersion defines the versioned schema of the PluginResponse that is back sent back to Kubebuilder.
// Initially, this will be marked as alpha (v1alpha1)
APIVersion string `json:"apiVersion"`

// Command holds the command that gets executed by the plugin such as init, create api, etc.
Command string `json:"command"`

// Metadata contains the plugin specific help text that the plugin returns to Kubebuilder when it receives
// `--help` flag from Kubebuilder.
Metadata plugin.SubcommandMetadata `json:"metadata"`

// Universe in the PluginResponse represents the updated file contents that was written by the plugin.
Universe map[string]string `json:"universe"`

// Error is a boolean type that indicates whether there were any errors due to plugin failures.
Error bool `json:"error,omitempty"`

// ErrorMsgs contains the specific error messages of the plugin failures.
ErrorMsgs []string `json:"errorMsgs,omitempty"`

// Flags contains the plugin specific flags that the plugin returns to Kubebuilder when it receives
// a request for a list of supported flags from Kubebuilder
Flags []Flag `json:"flags,omitempty"`

// Config stores the project configuration file.
Config string `json:"config"`
}

// Flag is meant to represent a CLI flag that is used by Kubebuilder to define flags that are parsed
// for use with an external plugin
type Flag struct {
// Name is the name that should be used when creating the flag.
// i.e a name of "domain" would become the CLI flag "--domain"
Name string

// Type is the type of flag that should be created. The types that
// Kubebuilder supports are: string, bool, int, and float.
// any value other than the supported will be defaulted to be a string
Type string

// Default is the default value that should be used for a flag.
// Kubebuilder will attempt to convert this value to the defined
// type for this flag.
Default string

// Usage is a description of the flag and when/why/what it is used for.
Usage string
}

type PluginConfig struct {
Resources []ResourceData `json:"resources,omitempty"`
}

type ResourceData struct {
Group string `json:"group,omitempty"`
Domain string `json:"domain,omitempty"`
Version string `json:"version"`
Kind string `json:"kind"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ require (
golang.org/x/sys v0.9.0 // indirect
golang.org/x/text v0.10.0 // indirect
golang.org/x/tools v0.10.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand All @@ -468,3 +469,4 @@ rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sigs.k8s.io/kubebuilder/v3 v3.11.1 h1:WbzjgZVIOYXJLRqal+j9YL4SjxjSvUuCas2j3NmzMq0=
sigs.k8s.io/kubebuilder/v3 v3.11.1/go.mod h1:4Re8w/tE0RsqR2IN5VnwCUgsbaIPk9DcYcZlRVALA2M=
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ limitations under the License.
package scaffolds

import (
"v1/external"
"v1/scaffolds/internal/templates/api"

"github.com/spf13/pflag"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
// "sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
)

var ApiFlags = []external.Flag{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ limitations under the License.
package scaffolds

import (
"v1/scaffolds/internal/templates"

"github.com/spf13/pflag"

"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
"sigs.k8s.io/yaml"

"v1/external"
"v1/scaffolds/internal/templates"
// "sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
)

var InitFlags = []external.Flag{
Expand Down Expand Up @@ -58,6 +59,40 @@ func InitCmd(pr *external.PluginRequest) external.PluginResponse {
flags.Parse(pr.Args)
domain, _ := flags.GetString("domain")

// Update the project config
cfg := external.PluginConfig{}
err := yaml.Unmarshal([]byte(pr.Config), &cfg)
if err != nil {
return external.PluginResponse{
Error: true,
ErrorMsgs: []string{
err.Error(),
},
}
}

// Create and append the new config info
newResource := external.ResourceData{
Group: "group",
Domain: "my.domain",
Version: "v1",
Kind: "Externalpluginsample",
}
cfg.Resources = append(cfg.Resources, newResource)

updatedConfig, err := yaml.Marshal(cfg)
if err != nil {
return external.PluginResponse{
Error: true,
ErrorMsgs: []string{
err.Error(),
},
}
}

// Update the PluginResponse with the modified config string
pluginResponse.Config = string(updatedConfig)

initFile := templates.NewInitFile(templates.WithDomain(domain))

// Phase 2 Plugins uses the concept of a "universe" to represent the filesystem for a plugin.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ package scaffolds
import (
"github.com/spf13/pflag"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
"v1/external"
// "sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
)

var WebhookFlags = []external.Flag{
Expand Down

0 comments on commit caac709

Please sign in to comment.