Skip to content

Commit d9d696d

Browse files
committed
replace crd-puller with pubres-toolkit
On-behalf-of: @SAP [email protected]
1 parent 1b5392e commit d9d696d

File tree

13 files changed

+419
-148
lines changed

13 files changed

+419
-148
lines changed

cmd/crd-puller/.gitignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

cmd/crd-puller/README.md

Lines changed: 0 additions & 18 deletions
This file was deleted.

cmd/crd-puller/main.go

Lines changed: 0 additions & 78 deletions
This file was deleted.

cmd/pubres-toolkit/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/pubres-toolkit
2+
*.yaml

cmd/pubres-toolkit/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# PublishedResource Toolkit
2+
3+
The `pubres-toolkit` can be used for testing and development in order to
4+
simulate how the agent would process a PublishedResource.
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
Copyright 2021 The KCP Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package crd
18+
19+
import (
20+
"context"
21+
"errors"
22+
"fmt"
23+
24+
"github.com/kcp-dev/api-syncagent/cmd/pubres-toolkit/util"
25+
"github.com/kcp-dev/api-syncagent/internal/discovery"
26+
"github.com/kcp-dev/api-syncagent/internal/kcp"
27+
"github.com/kcp-dev/api-syncagent/internal/projection"
28+
"github.com/spf13/cobra"
29+
"sigs.k8s.io/yaml"
30+
)
31+
32+
func NewCommand(ctx context.Context) *cobra.Command {
33+
opts := newDefaultOptions()
34+
35+
cmd := &cobra.Command{
36+
Use: "crd [--no-projection] [--ars] pubres.yaml",
37+
Short: "Outputs a CRD based on a PublishedResource",
38+
RunE: func(c *cobra.Command, args []string) error {
39+
if err := opts.Complete(); err != nil {
40+
return err
41+
}
42+
43+
if err := opts.Validate(); err != nil {
44+
return err
45+
}
46+
47+
if len(args) != 1 {
48+
return errors.New("expected exactly one argument")
49+
}
50+
51+
return run(ctx, opts, args[0])
52+
},
53+
}
54+
55+
opts.AddFlags(cmd.Flags())
56+
57+
return cmd
58+
}
59+
60+
func run(ctx context.Context, o *options, pubResFile string) error {
61+
// load the given PubRes
62+
pubResource, err := util.ReadPublishedResourceFile(pubResFile)
63+
if err != nil {
64+
return fmt.Errorf("failed to read %q: %w", pubResFile, err)
65+
}
66+
67+
// parse kubeconfig
68+
kubeconfig, err := util.ReadKubeconfig(o.KubeconfigFile, o.Context)
69+
if err != nil {
70+
return fmt.Errorf("invalid kubeconfig: %w", err)
71+
}
72+
73+
clientConfig, err := kubeconfig.ClientConfig()
74+
if err != nil {
75+
return err
76+
}
77+
78+
client, err := discovery.NewClient(clientConfig)
79+
if err != nil {
80+
return fmt.Errorf("failed to create discovery client: %w", err)
81+
}
82+
83+
// find the CRD that the PublishedResource is referring to
84+
localGK := projection.PublishedResourceSourceGK(pubResource)
85+
86+
// fetch the original, full CRD from the cluster
87+
crd, err := client.RetrieveCRD(ctx, localGK)
88+
if err != nil {
89+
return fmt.Errorf("failed to discover CRD defined in PublishedResource: %w", err)
90+
}
91+
92+
// project the CRD (i.e. strip unwanted versions, rename values etc.)
93+
if !o.NoProjection {
94+
crd, err = projection.ProjectCRD(crd, pubResource)
95+
if err != nil {
96+
return fmt.Errorf("failed to apply projection rules: %w", err)
97+
}
98+
}
99+
100+
// output the CRD right away if desired
101+
if !o.APIResourceSchema {
102+
enc, err := yaml.Marshal(crd)
103+
if err != nil {
104+
return fmt.Errorf("failed to encode CRD as YAML: %w", err)
105+
}
106+
107+
fmt.Println(string(enc))
108+
return nil
109+
}
110+
111+
// convert to APIResourceSchema otherwise
112+
arsName := kcp.GetAPIResourceSchemaName(crd)
113+
ars, err := kcp.CreateAPIResourceSchema(crd, arsName, "pubres-toolkit")
114+
if err != nil {
115+
return fmt.Errorf("failed to create APIResourceSchema: %w", err)
116+
}
117+
118+
enc, err := yaml.Marshal(ars)
119+
if err != nil {
120+
return fmt.Errorf("failed to encode APIResourceSchema as YAML: %w", err)
121+
}
122+
123+
fmt.Println(string(enc))
124+
125+
return nil
126+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright 2022 The KCP Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package crd
18+
19+
import (
20+
"fmt"
21+
"os"
22+
23+
"github.com/spf13/pflag"
24+
25+
utilerrors "k8s.io/apimachinery/pkg/util/errors"
26+
)
27+
28+
type options struct {
29+
KubeconfigFile string
30+
Context string
31+
32+
NoProjection bool
33+
APIResourceSchema bool
34+
}
35+
36+
func newDefaultOptions() *options {
37+
return &options{}
38+
}
39+
40+
func (o *options) AddFlags(flags *pflag.FlagSet) {
41+
flags.BoolVar(&o.NoProjection, "no-projection", o.NoProjection, "Disables projecting the GVK of the selected CRD")
42+
flags.BoolVar(&o.APIResourceSchema, "ars", o.APIResourceSchema, "Ouputs an APIResourceSchema instead of a CustomResourceDefinition")
43+
44+
flags.StringVar(&o.Context, "context", o.Context, "Name of the context in the kubeconfig file to use")
45+
flags.StringVar(&o.KubeconfigFile, "kubeconfig", o.KubeconfigFile, "The kubeconfig file of the cluster to read CRDs from (defaults to $KUBECONFIG).")
46+
}
47+
48+
func (o *options) Complete() error {
49+
errs := []error{}
50+
51+
if len(o.KubeconfigFile) == 0 {
52+
o.KubeconfigFile = os.Getenv("KUBECONFIG")
53+
}
54+
55+
return utilerrors.NewAggregate(errs)
56+
}
57+
58+
func (o *options) Validate() error {
59+
errs := []error{}
60+
61+
if len(o.KubeconfigFile) == 0 {
62+
errs = append(errs, fmt.Errorf("--kubeconfig or $KUBECONFIG are required for this command"))
63+
}
64+
65+
return utilerrors.NewAggregate(errs)
66+
}

cmd/pubres-toolkit/main.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright 2025 The KCP Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"context"
21+
"os"
22+
23+
crdcommand "github.com/kcp-dev/api-syncagent/cmd/pubres-toolkit/cmd/crd"
24+
25+
"k8s.io/component-base/cli"
26+
"k8s.io/component-base/version"
27+
28+
"github.com/spf13/cobra"
29+
)
30+
31+
func main() {
32+
ctx := context.Background()
33+
cmd := &cobra.Command{
34+
Use: "pubres-toolkit",
35+
Short: "Toolkit for PublishedResources",
36+
SilenceUsage: true,
37+
SilenceErrors: true,
38+
}
39+
40+
cmd.AddCommand(crdcommand.NewCommand(ctx))
41+
42+
if v := version.Get().String(); len(v) == 0 {
43+
cmd.Version = "<unknown>"
44+
} else {
45+
cmd.Version = v
46+
}
47+
48+
os.Exit(cli.Run(cmd))
49+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2025 The KCP Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import "k8s.io/client-go/tools/clientcmd"
20+
21+
func ReadKubeconfig(filename, context string) (clientcmd.ClientConfig, error) {
22+
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
23+
loadingRules.ExplicitPath = filename
24+
25+
startingConfig, err := loadingRules.GetStartingConfig()
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
overrides := &clientcmd.ConfigOverrides{
31+
CurrentContext: context,
32+
}
33+
34+
clientConfig := clientcmd.NewDefaultClientConfig(*startingConfig, overrides)
35+
return clientConfig, nil
36+
}

0 commit comments

Comments
 (0)