Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit 887aff8

Browse files
author
Andrew Rudoi
committed
chore: get kubeadm yaml rather than json
1 parent c3315b0 commit 887aff8

File tree

4 files changed

+93
-5
lines changed

4 files changed

+93
-5
lines changed

controllers/kubeadmconfig_controller.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package controllers
1818

1919
import (
2020
"context"
21-
"encoding/json"
2221
"fmt"
2322
"time"
2423

@@ -149,7 +148,7 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
149148
},
150149
}
151150
}
152-
initdata, err := json.Marshal(config.Spec.InitConfiguration)
151+
initdata, err := kubeadmv1beta1.ConfigurationToYAML(config.Spec.InitConfiguration)
153152
if err != nil {
154153
log.Error(err, "failed to marshal init configuration")
155154
return ctrl.Result{}, err
@@ -166,10 +165,10 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
166165
// If there is a control plane endpoint defined at cluster in cluster status, use it as a control plane endpoint for the K8s cluster
167166
// NB. we are only using the first one defined if there are multiple defined.
168167
if len(cluster.Status.APIEndpoints) > 0 {
169-
config.Spec.ClusterConfiguration.ControlPlaneEndpoint = fmt.Sprintf("https://%s:%d", cluster.Status.APIEndpoints[0].Host, cluster.Status.APIEndpoints[0].Port)
168+
config.Spec.ClusterConfiguration.ControlPlaneEndpoint = fmt.Sprintf("%s:%d", cluster.Status.APIEndpoints[0].Host, cluster.Status.APIEndpoints[0].Port)
170169
}
171170

172-
clusterdata, err := json.Marshal(config.Spec.ClusterConfiguration)
171+
clusterdata, err := kubeadmv1beta1.ConfigurationToYAML(config.Spec.ClusterConfiguration)
173172
if err != nil {
174173
log.Error(err, "failed to marshal cluster configuration")
175174
return ctrl.Result{}, err
@@ -215,7 +214,7 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
215214

216215
config.Spec.JoinConfiguration.Discovery.BootstrapToken.APIServerEndpoint = fmt.Sprintf("https://%s:%d", cluster.Status.APIEndpoints[0].Host, cluster.Status.APIEndpoints[0].Port)
217216

218-
joinBytes, err := json.Marshal(config.Spec.JoinConfiguration)
217+
joinBytes, err := kubeadmv1beta1.ConfigurationToYAML(config.Spec.JoinConfiguration)
219218
if err != nil {
220219
log.Error(err, "failed to marshal join configuration")
221220
return ctrl.Result{}, err

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible/go.mod h1:7v
238238
k8s.io/cluster-bootstrap v0.0.0-20190516232516-d7d78ab2cfe7 h1:5wvjieVoU4oovHlkeD256q2M2YYi2P01zk6wxSR2zk0=
239239
k8s.io/cluster-bootstrap v0.0.0-20190516232516-d7d78ab2cfe7/go.mod h1:iBSm2nwo3OaiuW8VDvc3ySDXK5SKfUrxwPvBloKG7zg=
240240
k8s.io/code-generator v0.0.0-20190311093542-50b561225d70/go.mod h1:MYiN+ZJZ9HkETbgVZdWw2AsuAi9PZ4V80cwfuf2axe8=
241+
k8s.io/component-base v0.0.0-20190409021516-bd2732e5c3f7 h1:f+AySqWvoqyCD7aArN3EZ+g2boKIS52pcSo6zdZkc+4=
241242
k8s.io/component-base v0.0.0-20190409021516-bd2732e5c3f7/go.mod h1:DMaomcf3j3MM2j1FsvlLVVlc7wA2jPytEur3cP9zRxQ=
242243
k8s.io/gengo v0.0.0-20190327210449-e17681d19d3a/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
243244
k8s.io/klog v0.2.0/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=

kubeadm/v1beta1/groupversion_info.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Copyright 2019 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 v1beta1
18+
19+
import (
20+
"k8s.io/apimachinery/pkg/runtime/schema"
21+
)
22+
23+
var (
24+
// GroupVersion is group version used to register these objects
25+
GroupVersion = schema.GroupVersion{Group: "kubeadm.k8s.io", Version: "v1beta1"}
26+
)

kubeadm/v1beta1/utils.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright 2019 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 v1beta1
18+
19+
import (
20+
"github.com/pkg/errors"
21+
runtime "k8s.io/apimachinery/pkg/runtime"
22+
"k8s.io/apimachinery/pkg/runtime/schema"
23+
"k8s.io/apimachinery/pkg/runtime/serializer"
24+
"sigs.k8s.io/controller-runtime/pkg/scheme"
25+
)
26+
27+
// GetCodecs returns a type that can be used to deserialize most kubeadm
28+
// configuration types.
29+
func GetCodecs() serializer.CodecFactory {
30+
sb := &scheme.Builder{GroupVersion: GroupVersion}
31+
32+
sb.Register(&JoinConfiguration{}, &InitConfiguration{}, &ClusterConfiguration{})
33+
kubeadmScheme, err := sb.Build()
34+
if err != nil {
35+
panic(err)
36+
}
37+
return serializer.NewCodecFactory(kubeadmScheme)
38+
}
39+
40+
// ConfigurationToYAML converts a kubeadm configuration type to its YAML
41+
// representation.
42+
func ConfigurationToYAML(obj runtime.Object) (string, error) {
43+
initcfg, err := MarshalToYamlForCodecs(obj, GroupVersion, GetCodecs())
44+
if err != nil {
45+
return "", errors.Wrap(err, "failed to marshal configuration")
46+
}
47+
return string(initcfg), nil
48+
}
49+
50+
// MarshalToYamlForCodecs marshals an object into yaml using the specified codec
51+
// TODO: Is specifying the gv really needed here?
52+
// TODO: Can we support json out of the box easily here?
53+
func MarshalToYamlForCodecs(obj runtime.Object, gv schema.GroupVersion, codecs serializer.CodecFactory) ([]byte, error) {
54+
mediaType := "application/yaml"
55+
info, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), mediaType)
56+
if !ok {
57+
return []byte{}, errors.Errorf("unsupported media type %q", mediaType)
58+
}
59+
60+
encoder := codecs.EncoderForVersion(info.Serializer, gv)
61+
return runtime.Encode(encoder, obj)
62+
}

0 commit comments

Comments
 (0)