Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions cmd/crd-puller/.gitignore

This file was deleted.

18 changes: 0 additions & 18 deletions cmd/crd-puller/README.md

This file was deleted.

78 changes: 0 additions & 78 deletions cmd/crd-puller/main.go

This file was deleted.

2 changes: 2 additions & 0 deletions cmd/pubres-toolkit/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/pubres-toolkit
*.yaml
4 changes: 4 additions & 0 deletions cmd/pubres-toolkit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# PublishedResource Toolkit

The `pubres-toolkit` can be used for testing and development in order to
simulate how the agent would process a PublishedResource.
128 changes: 128 additions & 0 deletions cmd/pubres-toolkit/cmd/crd/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
Copyright 2021 The KCP 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 crd

import (
"context"
"errors"
"fmt"

"github.com/spf13/cobra"

"github.com/kcp-dev/api-syncagent/cmd/pubres-toolkit/util"
"github.com/kcp-dev/api-syncagent/internal/discovery"
"github.com/kcp-dev/api-syncagent/internal/kcp"
"github.com/kcp-dev/api-syncagent/internal/projection"

"sigs.k8s.io/yaml"
)

func NewCommand(ctx context.Context) *cobra.Command {
opts := newDefaultOptions()

cmd := &cobra.Command{
Use: "crd [--no-projection] [--ars] pubres.yaml",
Short: "Outputs a CRD based on a PublishedResource",
RunE: func(c *cobra.Command, args []string) error {
if err := opts.Complete(); err != nil {
return err
}

if err := opts.Validate(); err != nil {
return err
}

if len(args) != 1 {
return errors.New("expected exactly one argument")
}

return run(ctx, opts, args[0])
},
}

opts.AddFlags(cmd.Flags())

return cmd
}

func run(ctx context.Context, o *options, pubResFile string) error {
// load the given PubRes
pubResource, err := util.ReadPublishedResourceFile(pubResFile)
if err != nil {
return fmt.Errorf("failed to read %q: %w", pubResFile, err)
}

// parse kubeconfig
kubeconfig, err := util.ReadKubeconfig(o.KubeconfigFile, o.Context)
if err != nil {
return fmt.Errorf("invalid kubeconfig: %w", err)
}

clientConfig, err := kubeconfig.ClientConfig()
if err != nil {
return err
}

client, err := discovery.NewClient(clientConfig)
if err != nil {
return fmt.Errorf("failed to create discovery client: %w", err)
}

// find the CRD that the PublishedResource is referring to
localGK := projection.PublishedResourceSourceGK(pubResource)

// fetch the original, full CRD from the cluster
crd, err := client.RetrieveCRD(ctx, localGK)
if err != nil {
return fmt.Errorf("failed to discover CRD defined in PublishedResource: %w", err)
}

// project the CRD (i.e. strip unwanted versions, rename values etc.)
if !o.NoProjection {
crd, err = projection.ProjectCRD(crd, pubResource)
if err != nil {
return fmt.Errorf("failed to apply projection rules: %w", err)
}
}

// output the CRD right away if desired
if !o.APIResourceSchema {
enc, err := yaml.Marshal(crd)
if err != nil {
return fmt.Errorf("failed to encode CRD as YAML: %w", err)
}

fmt.Println(string(enc))
return nil
}

// convert to APIResourceSchema otherwise
arsName := kcp.GetAPIResourceSchemaName(crd)
ars, err := kcp.CreateAPIResourceSchema(crd, arsName, "pubres-toolkit")
if err != nil {
return fmt.Errorf("failed to create APIResourceSchema: %w", err)
}

enc, err := yaml.Marshal(ars)
if err != nil {
return fmt.Errorf("failed to encode APIResourceSchema as YAML: %w", err)
}

fmt.Println(string(enc))

return nil
}
66 changes: 66 additions & 0 deletions cmd/pubres-toolkit/cmd/crd/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright 2022 The KCP 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 crd

import (
"fmt"
"os"

"github.com/spf13/pflag"

utilerrors "k8s.io/apimachinery/pkg/util/errors"
)

type options struct {
KubeconfigFile string
Context string

NoProjection bool
APIResourceSchema bool
}

func newDefaultOptions() *options {
return &options{}
}

func (o *options) AddFlags(flags *pflag.FlagSet) {
flags.BoolVar(&o.NoProjection, "no-projection", o.NoProjection, "Disables projecting the GVK of the selected CRD")
flags.BoolVar(&o.APIResourceSchema, "ars", o.APIResourceSchema, "Outputs an APIResourceSchema instead of a CustomResourceDefinition")

flags.StringVar(&o.Context, "context", o.Context, "Name of the context in the kubeconfig file to use")
flags.StringVar(&o.KubeconfigFile, "kubeconfig", o.KubeconfigFile, "The kubeconfig file of the cluster to read CRDs from (defaults to $KUBECONFIG).")
}

func (o *options) Complete() error {
errs := []error{}

if len(o.KubeconfigFile) == 0 {
o.KubeconfigFile = os.Getenv("KUBECONFIG")
}

return utilerrors.NewAggregate(errs)
}

func (o *options) Validate() error {
errs := []error{}

if len(o.KubeconfigFile) == 0 {
errs = append(errs, fmt.Errorf("--kubeconfig or $KUBECONFIG are required for this command"))
}

return utilerrors.NewAggregate(errs)
}
49 changes: 49 additions & 0 deletions cmd/pubres-toolkit/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2025 The KCP 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 (
"context"
"os"

"github.com/spf13/cobra"

crdcommand "github.com/kcp-dev/api-syncagent/cmd/pubres-toolkit/cmd/crd"

"k8s.io/component-base/cli"
"k8s.io/component-base/version"
)

func main() {
ctx := context.Background()
cmd := &cobra.Command{
Use: "pubres-toolkit",
Short: "Toolkit for PublishedResources",
SilenceUsage: true,
SilenceErrors: true,
}

cmd.AddCommand(crdcommand.NewCommand(ctx))

if v := version.Get().String(); len(v) == 0 {
cmd.Version = "<unknown>"
} else {
cmd.Version = v
}

os.Exit(cli.Run(cmd))
}
36 changes: 36 additions & 0 deletions cmd/pubres-toolkit/util/kubeconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2025 The KCP 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 util

import "k8s.io/client-go/tools/clientcmd"

func ReadKubeconfig(filename, context string) (clientcmd.ClientConfig, error) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
loadingRules.ExplicitPath = filename

startingConfig, err := loadingRules.GetStartingConfig()
if err != nil {
return nil, err
}

overrides := &clientcmd.ConfigOverrides{
CurrentContext: context,
}

clientConfig := clientcmd.NewDefaultClientConfig(*startingConfig, overrides)
return clientConfig, nil
}
Loading