Skip to content

Commit

Permalink
feat: add plugin entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
em-r committed Feb 6, 2023
1 parent c816d1b commit 3701b72
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 2 deletions.
4 changes: 2 additions & 2 deletions plugin/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import (
"errors"
"fmt"

"sigs.k8s.io/kubebuilder-declarative-pattern/plugin/v1/scaffolds"
"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/declarative/v1/scaffolds"
goPluginV2 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2"
)

Expand Down Expand Up @@ -111,7 +111,7 @@ func (p *createAPISubcommand) Scaffold(fs machinery.Filesystem) error {
if err := p.config.DecodePluginConfig(pluginKey, &cfg); errors.As(err, &config.UnsupportedFieldError{}) {
// Config doesn't support per-plugin configuration, so we can't track them
} else {
// Fail unless they key wasn't found, which just means it is the first resource tracked
// Fail unless the key wasn't found, which just means it is the first resource tracked
if err != nil && !errors.As(err, &config.PluginKeyNotFoundError{}) {
return err
}
Expand Down
85 changes: 85 additions & 0 deletions plugin/v1/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
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 cmd

import (
"bufio"
"encoding/json"
"fmt"
"io"
"log"
"os"

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

const (
commandInit = "init"
commandCreateApi = "create api"
)

func returnError(err error) {
response := external.PluginResponse{
Error: true,
ErrorMsgs: []string{err.Error()},
}

output, err := json.Marshal(response)
if err != nil {
log.Fatalf("encountered error serializing output: %s | output: %s", err.Error(), output)
}
fmt.Printf("%s", output)
}

// Run will run the steps defined by the plugin
func Run() {
// Kubebuilder makes requests to external plugin by writing STDIN.
reader := bufio.NewReader(os.Stdin)

input, err := io.ReadAll(reader)
if err != nil {
returnError(fmt.Errorf("encountered error reading from STDIN: %+v", err))
}

// Parsing request sent by kubebuilder into a PluginRequest instance.
pluginRequest := &external.PluginRequest{}

if err = json.Unmarshal(input, pluginRequest); err != nil {
returnError(err)
}

// Run logic based on the command executed by Kubebuilder.
var response external.PluginResponse
switch pluginRequest.Command {
case commandInit:
// handle init command
case commandCreateApi:
// handle create api command
default:
response = external.PluginResponse{
Error: true,
ErrorMsgs: []string{"unknown subcommand:" + pluginRequest.Command},
}
}

output, err := json.Marshal(response)
if err != nil {
returnError(err)
}

fmt.Printf("%s", output)
}
25 changes: 25 additions & 0 deletions plugin/v1/exec/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
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 (
"sigs.k8s.io/kubebuilder-declarative-pattern/plugin/v1/cmd"
)

func main() {
cmd.Run()
}
33 changes: 33 additions & 0 deletions plugin/v1/scaffolds/api-new.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
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 scaffolds

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

// ApiCmd handles all the logic for the `create api` subcommand
func ApiCmd(pr *external.PluginRequest) external.PluginResponse {
pluginResponse := external.PluginResponse{
Command: "init",
Universe: pr.Universe,
}

// TODO(@em-r): add logic for handling `create api` command

return pluginResponse
}
33 changes: 33 additions & 0 deletions plugin/v1/scaffolds/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
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 scaffolds

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

// InitCmd handles all the logic for the `init` subcommand
func InitCmd(pr *external.PluginRequest) external.PluginResponse {
pluginResponse := external.PluginResponse{
Command: "init",
Universe: pr.Universe,
}

// TODO(@em-r): add logic for handling `init` command

return pluginResponse
}

0 comments on commit 3701b72

Please sign in to comment.