Skip to content

Commit c7a90b6

Browse files
authored
Introduce kuberc as new flag to customize defaulting and define aliases in kubectl (kubernetes#125230)
1 parent 3d342e9 commit c7a90b6

File tree

20 files changed

+4603
-0
lines changed

20 files changed

+4603
-0
lines changed

pkg/generated/openapi/zz_generated.openapi.go

Lines changed: 236 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/kubectl/pkg/cmd/cmd.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import (
7373
cmdutil "k8s.io/kubectl/pkg/cmd/util"
7474
"k8s.io/kubectl/pkg/cmd/version"
7575
"k8s.io/kubectl/pkg/cmd/wait"
76+
"k8s.io/kubectl/pkg/kuberc"
7677
utilcomp "k8s.io/kubectl/pkg/util/completion"
7778
"k8s.io/kubectl/pkg/util/i18n"
7879
"k8s.io/kubectl/pkg/util/templates"
@@ -361,6 +362,11 @@ func NewKubectlCommand(o KubectlOptions) *cobra.Command {
361362

362363
flags.BoolVar(&warningsAsErrors, "warnings-as-errors", warningsAsErrors, "Treat warnings received from the server as errors and exit with a non-zero exit code")
363364

365+
pref := kuberc.NewPreferences()
366+
if cmdutil.KubeRC.IsEnabled() {
367+
pref.AddFlags(flags)
368+
}
369+
364370
kubeConfigFlags := o.ConfigFlags
365371
if kubeConfigFlags == nil {
366372
kubeConfigFlags = defaultConfigFlags().WithWarningPrinter(o.IOStreams)
@@ -490,6 +496,15 @@ func NewKubectlCommand(o KubectlOptions) *cobra.Command {
490496
// Stop warning about normalization of flags. That makes it possible to
491497
// add the klog flags later.
492498
cmds.SetGlobalNormalizationFunc(cliflag.WordSepNormalizeFunc)
499+
500+
if cmdutil.KubeRC.IsEnabled() {
501+
_, err := pref.Apply(cmds, o.Arguments, o.IOStreams.ErrOut)
502+
if err != nil {
503+
fmt.Fprintf(o.IOStreams.ErrOut, "error occurred while applying preferences %v\n", err)
504+
os.Exit(1)
505+
}
506+
}
507+
493508
return cmds
494509
}
495510

staging/src/k8s.io/kubectl/pkg/cmd/util/helpers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ const (
432432
PortForwardWebsockets FeatureGate = "KUBECTL_PORT_FORWARD_WEBSOCKETS"
433433
// DebugCustomProfile should be dropped in 1.34
434434
DebugCustomProfile FeatureGate = "KUBECTL_DEBUG_CUSTOM_PROFILE"
435+
KubeRC FeatureGate = "KUBECTL_KUBERC"
435436
)
436437

437438
// IsEnabled returns true iff environment variable is set to true.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# See the OWNERS docs at https://go.k8s.io/owners
2+
3+
# Disable inheritance as this is an api owners file
4+
options:
5+
no_parent_owners: true
6+
approvers:
7+
- api-approvers
8+
reviewers:
9+
- api-reviewers
10+
- sig-cli-reviewers
11+
labels:
12+
- kind/api-change
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Copyright 2024 The Kubernetes 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+
// +k8s:deepcopy-gen=package
18+
// +groupName=kubectl.config.k8s.io
19+
20+
package config // Package config import "k8s.io/kubectl/pkg/config"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright 2024 The Kubernetes 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 install installs the experimental API group, making it available as
18+
// an option to all of the API encoding/decoding machinery.
19+
package install
20+
21+
import (
22+
"k8s.io/apimachinery/pkg/runtime"
23+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
24+
"k8s.io/kubectl/pkg/config"
25+
"k8s.io/kubectl/pkg/config/v1alpha1"
26+
)
27+
28+
// Install registers the API group and adds types to a scheme
29+
func Install(scheme *runtime.Scheme) {
30+
utilruntime.Must(config.AddToScheme(scheme))
31+
utilruntime.Must(v1alpha1.AddToScheme(scheme))
32+
}

0 commit comments

Comments
 (0)