Skip to content

Commit efdb735

Browse files
committed
kubeadm: fix idempotency retain, address deprecation
CreateOrRetain is supposed to operate on an object name which isn't necessarily the given object's name (for use in migrations), this restores that feature. Replace all uses of deprecated functions with their generic variants. Providing the context externally isn't useful right now, drop it from the new functions and use context.Background() where needed. Signed-off-by: Stephen Kitt <[email protected]>
1 parent 29101e9 commit efdb735

File tree

12 files changed

+50
-145
lines changed

12 files changed

+50
-145
lines changed

cmd/kubeadm/app/discovery/token/token_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ type fakeConfigMap struct {
303303
}
304304

305305
func (c *fakeConfigMap) createOrUpdate(client clientset.Interface) error {
306-
return apiclient.CreateOrUpdateConfigMap(client, &v1.ConfigMap{
306+
return apiclient.CreateOrUpdate(client.CoreV1().ConfigMaps(metav1.NamespacePublic), &v1.ConfigMap{
307307
ObjectMeta: metav1.ObjectMeta{
308308
Name: c.name,
309309
Namespace: metav1.NamespacePublic,

cmd/kubeadm/app/phases/addons/dns/dns.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,11 @@ func createCoreDNSAddon(deploymentBytes, serviceBytes, configBytes []byte, clien
189189
// Assume that migration is always possible, rely on migrateCoreDNSCorefile() to fail if not.
190190
canMigrateCorefile := true
191191

192+
configMapClient := client.CoreV1().ConfigMaps(coreDNSConfigMap.GetNamespace())
193+
192194
if corefile == "" || migration.Default("", corefile) {
193195
// If the Corefile is empty or default, the latest default Corefile will be applied
194-
if err := apiclient.CreateOrUpdateConfigMap(client, coreDNSConfigMap); err != nil {
196+
if err := apiclient.CreateOrUpdate(configMapClient, coreDNSConfigMap); err != nil {
195197
return err
196198
}
197199
} else if corefileMigrationRequired {
@@ -201,13 +203,13 @@ func createCoreDNSAddon(deploymentBytes, serviceBytes, configBytes []byte, clien
201203
// to ignore preflight check errors.
202204
canMigrateCorefile = false
203205
klog.Warningf("the CoreDNS Configuration was not migrated: %v. The existing CoreDNS Corefile configuration has been retained.", err)
204-
if err := apiclient.CreateOrRetainConfigMap(client, coreDNSConfigMap, kubeadmconstants.CoreDNSConfigMap); err != nil {
206+
if err := apiclient.CreateOrRetain(configMapClient, coreDNSConfigMap, kubeadmconstants.CoreDNSConfigMap); err != nil {
205207
return err
206208
}
207209
}
208210
} else {
209211
// If the Corefile is modified and doesn't require any migration, it'll be retained for the benefit of the user
210-
if err := apiclient.CreateOrRetainConfigMap(client, coreDNSConfigMap, kubeadmconstants.CoreDNSConfigMap); err != nil {
212+
if err := apiclient.CreateOrRetain(configMapClient, coreDNSConfigMap, kubeadmconstants.CoreDNSConfigMap); err != nil {
211213
return err
212214
}
213215
}
@@ -218,7 +220,7 @@ func createCoreDNSAddon(deploymentBytes, serviceBytes, configBytes []byte, clien
218220
}
219221

220222
// Create the Clusterroles for CoreDNS or update it in case it already exists
221-
if err := apiclient.CreateOrUpdateClusterRole(client, coreDNSClusterRoles); err != nil {
223+
if err := apiclient.CreateOrUpdate(client.RbacV1().ClusterRoles(), coreDNSClusterRoles); err != nil {
222224
return err
223225
}
224226

@@ -228,7 +230,7 @@ func createCoreDNSAddon(deploymentBytes, serviceBytes, configBytes []byte, clien
228230
}
229231

230232
// Create the Clusterrolebindings for CoreDNS or update it in case it already exists
231-
if err := apiclient.CreateOrUpdateClusterRoleBinding(client, coreDNSClusterRolesBinding); err != nil {
233+
if err := apiclient.CreateOrUpdate(client.RbacV1().ClusterRoleBindings(), coreDNSClusterRolesBinding); err != nil {
232234
return err
233235
}
234236

@@ -238,7 +240,7 @@ func createCoreDNSAddon(deploymentBytes, serviceBytes, configBytes []byte, clien
238240
}
239241

240242
// Create the ConfigMap for CoreDNS or update it in case it already exists
241-
if err := apiclient.CreateOrUpdateServiceAccount(client, coreDNSServiceAccount); err != nil {
243+
if err := apiclient.CreateOrUpdate(client.CoreV1().ServiceAccounts(coreDNSServiceAccount.GetNamespace()), coreDNSServiceAccount); err != nil {
242244
return err
243245
}
244246

@@ -248,13 +250,14 @@ func createCoreDNSAddon(deploymentBytes, serviceBytes, configBytes []byte, clien
248250
}
249251

250252
// Create the deployment for CoreDNS or retain it in case the CoreDNS migration has failed during upgrade
253+
deploymentsClient := client.AppsV1().Deployments(coreDNSDeployment.GetNamespace())
251254
if !canMigrateCorefile {
252-
if err := apiclient.CreateOrRetainDeployment(client, coreDNSDeployment, kubeadmconstants.CoreDNSDeploymentName); err != nil {
255+
if err := apiclient.CreateOrRetain(deploymentsClient, coreDNSDeployment, kubeadmconstants.CoreDNSDeploymentName); err != nil {
253256
return err
254257
}
255258
} else {
256259
// Create the Deployment for CoreDNS or update it in case it already exists
257-
if err := apiclient.CreateOrUpdateDeployment(client, coreDNSDeployment); err != nil {
260+
if err := apiclient.CreateOrUpdate(deploymentsClient, coreDNSDeployment); err != nil {
258261
return err
259262
}
260263
}

cmd/kubeadm/app/phases/addons/proxy/proxy.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,19 @@ func printOrCreateKubeProxyObjects(cmByte []byte, dsByte []byte, client clientse
133133

134134
// Create the objects if printManifest is false
135135
if !printManifest {
136-
if err := apiclient.CreateOrUpdateServiceAccount(client, sa); err != nil {
136+
if err := apiclient.CreateOrUpdate(client.CoreV1().ServiceAccounts(sa.GetNamespace()), sa); err != nil {
137137
return errors.Wrap(err, "error when creating kube-proxy service account")
138138
}
139139

140-
if err := apiclient.CreateOrUpdateClusterRoleBinding(client, crb); err != nil {
140+
if err := apiclient.CreateOrUpdate(client.RbacV1().ClusterRoleBindings(), crb); err != nil {
141141
return err
142142
}
143143

144-
if err := apiclient.CreateOrUpdateRole(client, role); err != nil {
144+
if err := apiclient.CreateOrUpdate(client.RbacV1().Roles(role.GetNamespace()), role); err != nil {
145145
return err
146146
}
147147

148-
if err := apiclient.CreateOrUpdateRoleBinding(client, rb); err != nil {
148+
if err := apiclient.CreateOrUpdate(client.RbacV1().RoleBindings(rb.GetNamespace()), rb); err != nil {
149149
return err
150150
}
151151

@@ -243,7 +243,7 @@ func createKubeProxyConfigMap(cfg *kubeadmapi.ClusterConfiguration, localEndpoin
243243
}
244244

245245
// Create the ConfigMap for kube-proxy or update it in case it already exists
246-
return []byte(""), apiclient.CreateOrUpdateConfigMap(client, kubeproxyConfigMap)
246+
return []byte(""), apiclient.CreateOrUpdate(client.CoreV1().ConfigMaps(kubeproxyConfigMap.GetNamespace()), kubeproxyConfigMap)
247247
}
248248

249249
func createKubeProxyAddon(cfg *kubeadmapi.ClusterConfiguration, client clientset.Interface, printManifest bool) ([]byte, error) {
@@ -269,5 +269,5 @@ func createKubeProxyAddon(cfg *kubeadmapi.ClusterConfiguration, client clientset
269269
*env = append(*env, kubeadmutil.MergeKubeadmEnvVars(kubeadmutil.GetProxyEnvVars(nil))...)
270270

271271
// Create the DaemonSet for kube-proxy or update it in case it already exists
272-
return []byte(""), apiclient.CreateOrUpdateDaemonSet(client, kubeproxyDaemonSet)
272+
return []byte(""), apiclient.CreateOrUpdate(client.AppsV1().DaemonSets(kubeproxyDaemonSet.GetNamespace()), kubeproxyDaemonSet)
273273
}

cmd/kubeadm/app/phases/bootstraptoken/clusterinfo/clusterinfo.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func CreateBootstrapConfigMapIfNotExists(client clientset.Interface, kubeconfig
6969

7070
// Create or update the ConfigMap in the kube-public namespace
7171
klog.V(1).Infoln("[bootstrap-token] creating/updating ConfigMap in kube-public namespace")
72-
return apiclient.CreateOrUpdateConfigMap(client, &v1.ConfigMap{
72+
return apiclient.CreateOrUpdate(client.CoreV1().ConfigMaps(metav1.NamespacePublic), &v1.ConfigMap{
7373
ObjectMeta: metav1.ObjectMeta{
7474
Name: bootstrapapi.ConfigMapClusterInfo,
7575
Namespace: metav1.NamespacePublic,
@@ -83,7 +83,7 @@ func CreateBootstrapConfigMapIfNotExists(client clientset.Interface, kubeconfig
8383
// CreateClusterInfoRBACRules creates the RBAC rules for exposing the cluster-info ConfigMap in the kube-public namespace to unauthenticated users
8484
func CreateClusterInfoRBACRules(client clientset.Interface) error {
8585
klog.V(1).Infoln("creating the RBAC rules for exposing the cluster-info ConfigMap in the kube-public namespace")
86-
err := apiclient.CreateOrUpdateRole(client, &rbac.Role{
86+
err := apiclient.CreateOrUpdate(client.RbacV1().Roles(metav1.NamespacePublic), &rbac.Role{
8787
ObjectMeta: metav1.ObjectMeta{
8888
Name: BootstrapSignerClusterRoleName,
8989
Namespace: metav1.NamespacePublic,
@@ -101,7 +101,7 @@ func CreateClusterInfoRBACRules(client clientset.Interface) error {
101101
return err
102102
}
103103

104-
return apiclient.CreateOrUpdateRoleBinding(client, &rbac.RoleBinding{
104+
return apiclient.CreateOrUpdate(client.RbacV1().RoleBindings(metav1.NamespacePublic), &rbac.RoleBinding{
105105
ObjectMeta: metav1.ObjectMeta{
106106
Name: BootstrapSignerClusterRoleName,
107107
Namespace: metav1.NamespacePublic,

cmd/kubeadm/app/phases/bootstraptoken/node/tlsbootstrap.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
func AllowBootstrapTokensToPostCSRs(client clientset.Interface) error {
3232
fmt.Println("[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials")
3333

34-
return apiclient.CreateOrUpdateClusterRoleBinding(client, &rbac.ClusterRoleBinding{
34+
return apiclient.CreateOrUpdate(client.RbacV1().ClusterRoleBindings(), &rbac.ClusterRoleBinding{
3535
ObjectMeta: metav1.ObjectMeta{
3636
Name: constants.NodeKubeletBootstrap,
3737
},
@@ -53,7 +53,7 @@ func AllowBootstrapTokensToPostCSRs(client clientset.Interface) error {
5353
func AllowBootstrapTokensToGetNodes(client clientset.Interface) error {
5454
fmt.Println("[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to get nodes")
5555

56-
if err := apiclient.CreateOrUpdateClusterRole(client, &rbac.ClusterRole{
56+
if err := apiclient.CreateOrUpdate(client.RbacV1().ClusterRoles(), &rbac.ClusterRole{
5757
ObjectMeta: metav1.ObjectMeta{
5858
Name: constants.GetNodesClusterRoleName,
5959
},
@@ -68,7 +68,7 @@ func AllowBootstrapTokensToGetNodes(client clientset.Interface) error {
6868
return err
6969
}
7070

71-
return apiclient.CreateOrUpdateClusterRoleBinding(client, &rbac.ClusterRoleBinding{
71+
return apiclient.CreateOrUpdate(client.RbacV1().ClusterRoleBindings(), &rbac.ClusterRoleBinding{
7272
ObjectMeta: metav1.ObjectMeta{
7373
Name: constants.GetNodesClusterRoleName,
7474
},
@@ -91,7 +91,7 @@ func AutoApproveNodeBootstrapTokens(client clientset.Interface) error {
9191
fmt.Println("[bootstrap-token] Configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token")
9292

9393
// Always create this kubeadm-specific binding though
94-
return apiclient.CreateOrUpdateClusterRoleBinding(client, &rbac.ClusterRoleBinding{
94+
return apiclient.CreateOrUpdate(client.RbacV1().ClusterRoleBindings(), &rbac.ClusterRoleBinding{
9595
ObjectMeta: metav1.ObjectMeta{
9696
Name: constants.NodeAutoApproveBootstrapClusterRoleBinding,
9797
},
@@ -113,7 +113,7 @@ func AutoApproveNodeBootstrapTokens(client clientset.Interface) error {
113113
func AutoApproveNodeCertificateRotation(client clientset.Interface) error {
114114
fmt.Println("[bootstrap-token] Configured RBAC rules to allow certificate rotation for all node client certificates in the cluster")
115115

116-
return apiclient.CreateOrUpdateClusterRoleBinding(client, &rbac.ClusterRoleBinding{
116+
return apiclient.CreateOrUpdate(client.RbacV1().ClusterRoleBindings(), &rbac.ClusterRoleBinding{
117117
ObjectMeta: metav1.ObjectMeta{
118118
Name: constants.NodeAutoApproveCertificateRotationClusterRoleBinding,
119119
},

cmd/kubeadm/app/phases/bootstraptoken/node/token.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ func CreateNewTokens(client clientset.Interface, tokens []bootstraptokenv1.Boots
4040
// UpdateOrCreateTokens attempts to update a token with the given ID, or create if it does not already exist.
4141
func UpdateOrCreateTokens(client clientset.Interface, failIfExists bool, tokens []bootstraptokenv1.BootstrapToken) error {
4242

43+
secretsClient := client.CoreV1().Secrets(metav1.NamespaceSystem)
44+
4345
for _, token := range tokens {
4446

4547
secretName := bootstraputil.BootstrapTokenSecretName(token.Token.ID)
46-
secret, err := client.CoreV1().Secrets(metav1.NamespaceSystem).Get(context.TODO(), secretName, metav1.GetOptions{})
48+
secret, err := secretsClient.Get(context.Background(), secretName, metav1.GetOptions{})
4749
if secret != nil && err == nil && failIfExists {
4850
return errors.Errorf("a token with id %q already exists", token.Token.ID)
4951
}
@@ -56,7 +58,7 @@ func UpdateOrCreateTokens(client clientset.Interface, failIfExists bool, tokens
5658
kubeadmconstants.KubernetesAPICallRetryInterval,
5759
kubeadmapi.GetActiveTimeouts().KubernetesAPICall.Duration,
5860
true, func(_ context.Context) (bool, error) {
59-
if err := apiclient.CreateOrUpdateSecret(client, updatedOrNewSecret); err != nil {
61+
if err := apiclient.CreateOrUpdate(secretsClient, updatedOrNewSecret); err != nil {
6062
lastError = errors.Wrapf(err, "failed to create or update bootstrap token with name %s", secretName)
6163
return false, nil
6264
}

cmd/kubeadm/app/phases/copycerts/copycerts.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func UploadCerts(client clientset.Interface, cfg *kubeadmapi.InitConfiguration,
106106
return err
107107
}
108108

109-
err = apiclient.CreateOrUpdateSecret(client, &v1.Secret{
109+
err = apiclient.CreateOrUpdate(client.CoreV1().Secrets(metav1.NamespaceSystem), &v1.Secret{
110110
ObjectMeta: metav1.ObjectMeta{
111111
Name: kubeadmconstants.KubeadmCertsSecret,
112112
Namespace: metav1.NamespaceSystem,
@@ -122,7 +122,7 @@ func UploadCerts(client clientset.Interface, cfg *kubeadmapi.InitConfiguration,
122122
}
123123

124124
func createRBAC(client clientset.Interface) error {
125-
err := apiclient.CreateOrUpdateRole(client, &rbac.Role{
125+
err := apiclient.CreateOrUpdate(client.RbacV1().Roles(metav1.NamespaceSystem), &rbac.Role{
126126
ObjectMeta: metav1.ObjectMeta{
127127
Name: kubeadmconstants.KubeadmCertsClusterRoleName,
128128
Namespace: metav1.NamespaceSystem,
@@ -140,7 +140,7 @@ func createRBAC(client clientset.Interface) error {
140140
return err
141141
}
142142

143-
return apiclient.CreateOrUpdateRoleBinding(client, &rbac.RoleBinding{
143+
return apiclient.CreateOrUpdate(client.RbacV1().RoleBindings(metav1.NamespaceSystem), &rbac.RoleBinding{
144144
ObjectMeta: metav1.ObjectMeta{
145145
Name: kubeadmconstants.KubeadmCertsClusterRoleName,
146146
Namespace: metav1.NamespaceSystem,

cmd/kubeadm/app/phases/kubelet/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func CreateConfigMap(cfg *kubeadmapi.ClusterConfiguration, client clientset.Inte
151151
componentconfigs.SignConfigMap(configMap)
152152
}
153153

154-
if err := apiclient.CreateOrUpdateConfigMap(client, configMap); err != nil {
154+
if err := apiclient.CreateOrUpdate(client.CoreV1().ConfigMaps(configMap.GetNamespace()), configMap); err != nil {
155155
return err
156156
}
157157

@@ -163,7 +163,7 @@ func CreateConfigMap(cfg *kubeadmapi.ClusterConfiguration, client clientset.Inte
163163

164164
// createConfigMapRBACRules creates the RBAC rules for exposing the base kubelet ConfigMap in the kube-system namespace to unauthenticated users
165165
func createConfigMapRBACRules(client clientset.Interface) error {
166-
if err := apiclient.CreateOrUpdateRole(client, &rbac.Role{
166+
if err := apiclient.CreateOrUpdate(client.RbacV1().Roles(metav1.NamespaceSystem), &rbac.Role{
167167
ObjectMeta: metav1.ObjectMeta{
168168
Name: kubeadmconstants.KubeletBaseConfigMapRole,
169169
Namespace: metav1.NamespaceSystem,
@@ -180,7 +180,7 @@ func createConfigMapRBACRules(client clientset.Interface) error {
180180
return err
181181
}
182182

183-
return apiclient.CreateOrUpdateRoleBinding(client, &rbac.RoleBinding{
183+
return apiclient.CreateOrUpdate(client.RbacV1().RoleBindings(metav1.NamespaceSystem), &rbac.RoleBinding{
184184
ObjectMeta: metav1.ObjectMeta{
185185
Name: kubeadmconstants.KubeletBaseConfigMapRole,
186186
Namespace: metav1.NamespaceSystem,

cmd/kubeadm/app/phases/uploadconfig/uploadconfig.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func UploadConfiguration(cfg *kubeadmapi.InitConfiguration, client clientset.Int
5959
return err
6060
}
6161

62-
err = apiclient.CreateOrMutateConfigMap(client, &v1.ConfigMap{
62+
err = apiclient.CreateOrMutate(client.CoreV1().ConfigMaps(metav1.NamespaceSystem), &v1.ConfigMap{
6363
ObjectMeta: metav1.ObjectMeta{
6464
Name: kubeadmconstants.KubeadmConfigConfigMap,
6565
Namespace: metav1.NamespaceSystem,
@@ -78,7 +78,7 @@ func UploadConfiguration(cfg *kubeadmapi.InitConfiguration, client clientset.Int
7878
}
7979

8080
// Ensure that the NodesKubeadmConfigClusterRoleName exists
81-
err = apiclient.CreateOrUpdateRole(client, &rbac.Role{
81+
err = apiclient.CreateOrUpdate(client.RbacV1().Roles(metav1.NamespaceSystem), &rbac.Role{
8282
ObjectMeta: metav1.ObjectMeta{
8383
Name: NodesKubeadmConfigClusterRoleName,
8484
Namespace: metav1.NamespaceSystem,
@@ -99,7 +99,7 @@ func UploadConfiguration(cfg *kubeadmapi.InitConfiguration, client clientset.Int
9999
// Binds the NodesKubeadmConfigClusterRoleName to all the bootstrap tokens
100100
// that are members of the system:bootstrappers:kubeadm:default-node-token group
101101
// and to all nodes
102-
return apiclient.CreateOrUpdateRoleBinding(client, &rbac.RoleBinding{
102+
return apiclient.CreateOrUpdate(client.RbacV1().RoleBindings(metav1.NamespaceSystem), &rbac.RoleBinding{
103103
ObjectMeta: metav1.ObjectMeta{
104104
Name: NodesKubeadmConfigClusterRoleName,
105105
Namespace: metav1.NamespaceSystem,

0 commit comments

Comments
 (0)