kubeadm: move token defaulting and related constants to token package

Move the defaulting of the BootstrapToken type inside the
bootstraptoken/v1 package. This prevents an error where
codegen complains that a defaulter for the type exists in both
the kubeadm v1beta3 and v1beta4.

Adapt kubeadm code to use the defaulter function and constants
that were moved to bootstraptoken/v1.

NOTE: technically this is a breaking change for direct users of
v1beta3/SetDefaults_BootstrapToken().
This commit is contained in:
Lubomir I. Ivanov 2023-06-21 13:33:26 +03:00
parent eecac05871
commit edb59e9bc7
8 changed files with 62 additions and 60 deletions

View File

@ -0,0 +1,54 @@
/*
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 v1
import (
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
bootstrapapi "k8s.io/cluster-bootstrap/token/api"
)
const (
// DefaultTokenDuration specifies the default amount of time that a bootstrap token will be valid
// Default behaviour is 24 hours
DefaultTokenDuration = 24 * time.Hour
)
var (
// DefaultTokenUsages specifies the default functions a token will get
DefaultTokenUsages = bootstrapapi.KnownTokenUsages
// DefaultTokenGroups specifies the default groups that this token will authenticate as when used for authentication
DefaultTokenGroups = []string{"system:bootstrappers:kubeadm:default-node-token"}
)
// SetDefaults_BootstrapToken sets the defaults for an individual Bootstrap Token
func SetDefaults_BootstrapToken(bt *BootstrapToken) {
if bt.TTL == nil {
bt.TTL = &metav1.Duration{
Duration: DefaultTokenDuration,
}
}
if len(bt.Usages) == 0 {
bt.Usages = DefaultTokenUsages
}
if len(bt.Groups) == 0 {
bt.Groups = DefaultTokenGroups
}
}

View File

@ -181,23 +181,7 @@ func SetDefaults_BootstrapTokens(obj *InitConfiguration) {
}
for i := range obj.BootstrapTokens {
SetDefaults_BootstrapToken(&obj.BootstrapTokens[i])
}
}
// SetDefaults_BootstrapToken sets the defaults for an individual Bootstrap Token
func SetDefaults_BootstrapToken(bt *bootstraptokenv1.BootstrapToken) {
if bt.TTL == nil {
bt.TTL = &metav1.Duration{
Duration: constants.DefaultTokenDuration,
}
}
if len(bt.Usages) == 0 {
bt.Usages = constants.DefaultTokenUsages
}
if len(bt.Groups) == 0 {
bt.Groups = constants.DefaultTokenGroups
bootstraptokenv1.SetDefaults_BootstrapToken(&obj.BootstrapTokens[i])
}
}

View File

@ -42,10 +42,6 @@ func SetObjectDefaults_ClusterConfiguration(in *ClusterConfiguration) {
func SetObjectDefaults_InitConfiguration(in *InitConfiguration) {
SetDefaults_InitConfiguration(in)
for i := range in.BootstrapTokens {
a := &in.BootstrapTokens[i]
SetDefaults_BootstrapToken(a)
}
SetDefaults_APIEndpoint(&in.LocalAPIEndpoint)
}

View File

@ -181,23 +181,7 @@ func SetDefaults_BootstrapTokens(obj *InitConfiguration) {
}
for i := range obj.BootstrapTokens {
SetDefaults_BootstrapToken(&obj.BootstrapTokens[i])
}
}
// SetDefaults_BootstrapToken sets the defaults for an individual Bootstrap Token
func SetDefaults_BootstrapToken(bt *bootstraptokenv1.BootstrapToken) {
if bt.TTL == nil {
bt.TTL = &metav1.Duration{
Duration: constants.DefaultTokenDuration,
}
}
if len(bt.Usages) == 0 {
bt.Usages = constants.DefaultTokenUsages
}
if len(bt.Groups) == 0 {
bt.Groups = constants.DefaultTokenGroups
bootstraptokenv1.SetDefaults_BootstrapToken(&obj.BootstrapTokens[i])
}
}

View File

@ -42,10 +42,6 @@ func SetObjectDefaults_ClusterConfiguration(in *ClusterConfiguration) {
func SetObjectDefaults_InitConfiguration(in *InitConfiguration) {
SetDefaults_InitConfiguration(in)
for i := range in.BootstrapTokens {
a := &in.BootstrapTokens[i]
SetDefaults_BootstrapToken(a)
}
SetDefaults_APIEndpoint(&in.LocalAPIEndpoint)
}

View File

@ -27,7 +27,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
v1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/bootstraptoken/v1"
bootstraptokenv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/bootstraptoken/v1"
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
@ -121,12 +121,12 @@ func TestNewInitData(t *testing.T) {
AdvertiseAddress: "1.2.3.4",
BindPort: 6443,
},
BootstrapTokens: []v1.BootstrapToken{
BootstrapTokens: []bootstraptokenv1.BootstrapToken{
{
Token: &v1.BootstrapTokenString{ID: "abcdef", Secret: "0123456789abcdef"},
Token: &bootstraptokenv1.BootstrapTokenString{ID: "abcdef", Secret: "0123456789abcdef"},
Usages: []string{"signing", "authentication"},
TTL: &metav1.Duration{
Duration: constants.DefaultTokenDuration,
Duration: bootstraptokenv1.DefaultTokenDuration,
},
Groups: []string{"system:bootstrappers:kubeadm:default-node-token"},
},

View File

@ -26,13 +26,12 @@ import (
bootstraptokenv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/bootstraptoken/v1"
kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
)
// NewBootstrapTokenOptions creates a new BootstrapTokenOptions object with the default values
func NewBootstrapTokenOptions() *BootstrapTokenOptions {
bto := &BootstrapTokenOptions{&bootstraptokenv1.BootstrapToken{}, ""}
kubeadmapiv1.SetDefaults_BootstrapToken(bto.BootstrapToken)
bootstraptokenv1.SetDefaults_BootstrapToken(bto.BootstrapToken)
return bto
}
@ -69,7 +68,7 @@ func (bto *BootstrapTokenOptions) AddTTLFlagWithName(fs *pflag.FlagSet, flagName
func (bto *BootstrapTokenOptions) AddUsagesFlag(fs *pflag.FlagSet) {
fs.StringSliceVar(
&bto.Usages, TokenUsages, bto.Usages,
fmt.Sprintf("Describes the ways in which this token can be used. You can pass --usages multiple times or provide a comma separated list of options. Valid options: [%s]", strings.Join(kubeadmconstants.DefaultTokenUsages, ",")),
fmt.Sprintf("Describes the ways in which this token can be used. You can pass --usages multiple times or provide a comma separated list of options. Valid options: [%s]", strings.Join(bootstraptokenv1.DefaultTokenUsages, ",")),
)
}

View File

@ -31,7 +31,6 @@ import (
"k8s.io/apimachinery/pkg/util/version"
"k8s.io/apimachinery/pkg/util/wait"
apimachineryversion "k8s.io/apimachinery/pkg/version"
bootstrapapi "k8s.io/cluster-bootstrap/token/api"
componentversion "k8s.io/component-base/version"
netutils "k8s.io/utils/net"
)
@ -243,10 +242,6 @@ const (
// The node subnet mask size must be no more than the pod subnet mask size + 16
PodSubnetNodeMaskMaxDiff = 16
// DefaultTokenDuration specifies the default amount of time that a bootstrap token will be valid
// Default behaviour is 24 hours
DefaultTokenDuration = 24 * time.Hour
// DefaultCertTokenDuration specifies the default amount of time that the token used by upload certs will be valid
// Default behaviour is 2 hours
DefaultCertTokenDuration = 2 * time.Hour
@ -447,12 +442,6 @@ var (
Effect: v1.TaintEffectNoSchedule,
}
// DefaultTokenUsages specifies the default functions a token will get
DefaultTokenUsages = bootstrapapi.KnownTokenUsages
// DefaultTokenGroups specifies the default groups that this token will authenticate as when used for authentication
DefaultTokenGroups = []string{NodeBootstrapTokenAuthGroup}
// ControlPlaneComponents defines the control-plane component names
ControlPlaneComponents = []string{KubeAPIServer, KubeControllerManager, KubeScheduler}