Skip to content

Commit c832fc4

Browse files
committed
feat(claimtoxr): add Options API with Name and UID
Replace ConvertClaimToXR's args with an Options struct, adding two new ones: - Name: explicit XR name, overrides the direct-mode default (Claim name) or the non-direct random suffix - GenerateUID: set `metadata.uid` via `uuid.NewUUID()` Existing Kind and Direct arguments are unchanged. The runner's call site uses Options{} (without values) matching the previous behavior. Also, the function now reutnrs `*composite.Unstructured` rather thant `*unstructured.Unstructured`, since the produced object is conceptually a Composite Resource. YAML/JSON marshaling continues to work via the embedded `unstructured.Unstructured`. The `xprin-helpers convert-claim-to-xr` command exposes the new options as `--name` and `--gen-uid` flags. Documentation is updated accordingly. Signed-off-by: Theo Chatzimichos <tampakrap@gmail.com>
1 parent b5a100a commit c832fc4

6 files changed

Lines changed: 201 additions & 88 deletions

File tree

‎cmd/xprin-helpers/claimtoxr/cmd.go‎

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ type Cmd struct {
3737
InputFile string `arg:"" default:"-" help:"The Claim YAML file to be converted. If not specified or '-', stdin will be used." optional:"" predictor:"file" type:"path"`
3838

3939
// Flags.
40-
OutputFile string `help:"The file to write the generated XR YAML to. If not specified, stdout will be used." placeholder:"PATH" predictor:"file" short:"o" type:"path"`
41-
Kind string `help:"The kind to use for the XR. If not specified, 'X' will be prepended to the Claim's kind (e.g. Infra -> XInfra)." placeholder:"KIND" type:"string"`
42-
Direct bool `help:"Create a direct XR without Claim references and suffix." name:"direct" negatable:""`
40+
OutputFile string `help:"The file to write the generated XR YAML to. If not specified, stdout will be used." placeholder:"PATH" predictor:"file" short:"o" type:"path"`
41+
Name string `help:"The name to use for the XR. If empty, defaults to the Claim's name (direct mode) or the Claim's name with a random suffix (non-direct)." placeholder:"NAME" type:"string"`
42+
Kind string `help:"The kind to use for the XR. If not specified, 'X' will be prepended to the Claim's kind (e.g. Infra -> XInfra)." placeholder:"KIND" type:"string"`
43+
Direct bool `help:"Create a direct XR without Claim references and suffix." name:"direct" negatable:""`
44+
GenUID bool `help:"Set a fresh random metadata.uid on the generated XR." name:"gen-uid"`
4345

4446
fs afero.Fs
4547
}
@@ -50,26 +52,32 @@ func (c *Cmd) Help() string {
5052
Convert a Crossplane Claim YAML file to a Crossplane Composite Resource (XR) format.
5153
5254
This command will:
53-
- Read the Claim from the provided YAML file
54-
- Create an XR with the same spec as the Claim
55-
- Set appropriate API version and kind for the XR
56-
- Set up the Claim reference in the XR (unless --direct is used)
57-
- Copy any composition selector
55+
- Read the Claim from the provided YAML file
56+
- Create an XR with the same spec as the Claim
57+
- Set appropriate API version and kind for the XR
58+
- Set up the Claim reference in the XR (unless --direct is used)
59+
- Copy any composition selector
5860
5961
Examples:
6062
6163
# Convert claim.yaml to XR format and write to stdout (kind will be 'X' + Claim's kind)
6264
xprin-helpers convert-claim-to-xr claim.yaml
6365
64-
# Convert claim.yaml to XR format with a specific kind
65-
xprin-helpers convert-claim-to-xr claim.yaml --kind MyCompositeResource
66-
6766
# Convert claim.yaml to XR format and write to xr.yaml
6867
xprin-helpers convert-claim-to-xr claim.yaml -o xr.yaml
6968
69+
# Convert claim.yaml using an explicit XR name (overrides the default suffix or claim name)
70+
xprin-helpers convert-claim-to-xr claim.yaml --name my-xr
71+
72+
# Convert claim.yaml to XR format with a specific kind
73+
xprin-helpers convert-claim-to-xr claim.yaml --kind MyCompositeResource
74+
7075
# Convert claim.yaml to a directly created XR (no Claim references, no name suffix)
7176
xprin-helpers convert-claim-to-xr claim.yaml --direct
7277
78+
# Convert claim.yaml and assign a fresh random metadata.uid to the XR
79+
xprin-helpers convert-claim-to-xr claim.yaml --gen-uid
80+
7381
# Convert Claim from stdin to XR format
7482
cat claim.yaml | xprin-helpers convert-claim-to-xr -
7583
`
@@ -94,7 +102,12 @@ func (c *Cmd) Run(k *kong.Context) error {
94102
}
95103

96104
// Convert to XR
97-
xr, err := ConvertClaimToXR(claim, c.Kind, c.Direct)
105+
xr, err := ConvertClaimToXR(claim, Options{
106+
Name: c.Name,
107+
Kind: c.Kind,
108+
Direct: c.Direct,
109+
GenerateUID: c.GenUID,
110+
})
98111
if err != nil {
99112
return errors.Wrap(err, "failed to convert Claim to XR")
100113
}

‎cmd/xprin-helpers/claimtoxr/converter.go‎

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package claimtoxr
1919
import (
2020
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2121
"k8s.io/apimachinery/pkg/runtime/schema"
22+
"k8s.io/apimachinery/pkg/util/uuid"
2223
"k8s.io/apiserver/pkg/storage/names"
2324

2425
"github.com/crossplane/crossplane-runtime/v2/pkg/errors"
@@ -41,8 +42,28 @@ const (
4142
labelComposite = "crossplane.io/composite"
4243
)
4344

45+
// Options configures ConvertClaimToXR.
46+
type Options struct {
47+
// Name is the XR name. Empty falls back to:
48+
// - claim.Name when Direct is true
49+
// - claim.Name with a random suffix when Direct is false
50+
// A non-empty Name overrides both fallbacks.
51+
Name string
52+
53+
// Kind is the XR kind. Empty defaults to "X" + claim.Kind.
54+
Kind string
55+
56+
// Direct controls XR linkage to the claim:
57+
// - true: no spec.claimRef; no claim-name/claim-namespace labels
58+
// - false: spec.claimRef is set; claim-name/claim-namespace labels added
59+
Direct bool
60+
61+
// GenerateUID, when true, sets metadata.uid to a fresh random UUID.
62+
GenerateUID bool
63+
}
64+
4465
// ConvertClaimToXR converts a Crossplane Claim to a Composite Resource (XR).
45-
func ConvertClaimToXR(claim *unstructured.Unstructured, kind string, direct bool) (*unstructured.Unstructured, error) {
66+
func ConvertClaimToXR(claim *unstructured.Unstructured, opts Options) (*composite.Unstructured, error) {
4667
if claim == nil {
4768
return nil, errors.New(errNilInput)
4869
}
@@ -92,7 +113,8 @@ func ConvertClaimToXR(claim *unstructured.Unstructured, kind string, direct bool
92113
return nil, errors.Wrap(err, "failed to set apiVersion")
93114
}
94115

95-
// Set XR kind - either from flag or by prepending X to Claim's kind
116+
// Set XR kind - either from opts or by prepending X to Claim's kind
117+
kind := opts.Kind
96118
if kind == "" {
97119
kind = "X" + claimKind
98120
}
@@ -113,7 +135,7 @@ func ConvertClaimToXR(claim *unstructured.Unstructured, kind string, direct bool
113135

114136
xrName := claimName
115137

116-
if !direct {
138+
if !opts.Direct {
117139
xrName = names.SimpleNameGenerator.GenerateName(claimName + "-")
118140
labels[labelClaimName] = claim.GetName()
119141

@@ -128,6 +150,11 @@ func ConvertClaimToXR(claim *unstructured.Unstructured, kind string, direct bool
128150
}
129151
}
130152

153+
// Explicit Name overrides both Direct's claim-name default and the generated suffix.
154+
if opts.Name != "" {
155+
xrName = opts.Name
156+
}
157+
131158
if err := xrPaved.SetString("metadata.name", xrName); err != nil {
132159
return nil, errors.Wrap(err, "failed to set name")
133160
}
@@ -140,6 +167,9 @@ func ConvertClaimToXR(claim *unstructured.Unstructured, kind string, direct bool
140167
}
141168
}
142169

143-
// Convert from composite.Unstructured to unstructured.Unstructured
144-
return &unstructured.Unstructured{Object: xr.UnstructuredContent()}, nil
170+
if opts.GenerateUID {
171+
xr.SetUID(uuid.NewUUID())
172+
}
173+
174+
return xr, nil
145175
}

0 commit comments

Comments
 (0)