mirror of https://github.com/k3s-io/k3s
Merge pull request #55380 from xiangpengzhao/kubeadm-util
Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Use volumeutil.LoadPodFromFile for pod spec **What this PR does / why we need it**: Minor cleanup. **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: Fixes # **Special notes for your reviewer**: /cc @luxas **Release note**: ```release-note NONE ```pull/6/head
commit
aedba56cad
|
@ -18,6 +18,7 @@ go_test(
|
|||
deps = [
|
||||
"//cmd/kubeadm/app/constants:go_default_library",
|
||||
"//cmd/kubeadm/app/util:go_default_library",
|
||||
"//pkg/volume/util:go_default_library",
|
||||
"//vendor/k8s.io/api/apps/v1beta2:go_default_library",
|
||||
"//vendor/k8s.io/api/core/v1:go_default_library",
|
||||
],
|
||||
|
@ -37,11 +38,10 @@ go_library(
|
|||
"//cmd/kubeadm/app/features:go_default_library",
|
||||
"//cmd/kubeadm/app/util:go_default_library",
|
||||
"//cmd/kubeadm/app/util/apiclient:go_default_library",
|
||||
"//pkg/api/legacyscheme:go_default_library",
|
||||
"//pkg/volume/util:go_default_library",
|
||||
"//vendor/k8s.io/api/apps/v1beta2:go_default_library",
|
||||
"//vendor/k8s.io/api/core/v1:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||
"//vendor/k8s.io/client-go/kubernetes:go_default_library",
|
||||
],
|
||||
)
|
||||
|
|
|
@ -18,20 +18,18 @@ package selfhosting
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
apps "k8s.io/api/apps/v1beta2"
|
||||
"k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
kuberuntime "k8s.io/apimachinery/pkg/runtime"
|
||||
clientset "k8s.io/client-go/kubernetes"
|
||||
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
||||
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
||||
"k8s.io/kubernetes/cmd/kubeadm/app/features"
|
||||
"k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
|
||||
"k8s.io/kubernetes/pkg/api/legacyscheme"
|
||||
volumeutil "k8s.io/kubernetes/pkg/volume/util"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -59,7 +57,7 @@ func CreateSelfHostedControlPlane(manifestsDir, kubeConfigDir string, cfg *kubea
|
|||
// Adjust the timeout slightly to something self-hosting specific
|
||||
waiter.SetTimeout(selfHostingWaitTimeout)
|
||||
|
||||
// Here the map of different mutators to use for the control plane's podspec is stored
|
||||
// Here the map of different mutators to use for the control plane's PodSpec is stored
|
||||
mutators := GetMutatorsFromFeatureGates(cfg.FeatureGates)
|
||||
|
||||
// Some extra work to be done if we should store the control plane certificates in Secrets
|
||||
|
@ -85,10 +83,11 @@ func CreateSelfHostedControlPlane(manifestsDir, kubeConfigDir string, cfg *kubea
|
|||
}
|
||||
|
||||
// Load the Static Pod file in order to be able to create a self-hosted variant of that file
|
||||
podSpec, err := loadPodSpecFromFile(manifestPath)
|
||||
pod, err := volumeutil.LoadPodFromFile(manifestPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
podSpec := &pod.Spec
|
||||
|
||||
// Build a DaemonSet object from the loaded PodSpec
|
||||
ds := BuildDaemonSet(componentName, podSpec, mutators)
|
||||
|
@ -156,22 +155,6 @@ func BuildDaemonSet(name string, podSpec *v1.PodSpec, mutators map[string][]PodS
|
|||
}
|
||||
}
|
||||
|
||||
// loadPodSpecFromFile reads and decodes a file containing a specification of a Pod
|
||||
// TODO: Consider using "k8s.io/kubernetes/pkg/volume/util".LoadPodFromFile(filename string) in the future instead.
|
||||
func loadPodSpecFromFile(manifestPath string) (*v1.PodSpec, error) {
|
||||
podBytes, err := ioutil.ReadFile(manifestPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
staticPod := &v1.Pod{}
|
||||
if err := kuberuntime.DecodeInto(legacyscheme.Codecs.UniversalDecoder(), podBytes, staticPod); err != nil {
|
||||
return nil, fmt.Errorf("unable to decode static pod %v", err)
|
||||
}
|
||||
|
||||
return &staticPod.Spec, nil
|
||||
}
|
||||
|
||||
// BuildSelfhostedComponentLabels returns the labels for a self-hosted component
|
||||
func BuildSelfhostedComponentLabels(component string) map[string]string {
|
||||
return map[string]string{
|
||||
|
|
|
@ -26,6 +26,7 @@ import (
|
|||
apps "k8s.io/api/apps/v1beta2"
|
||||
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
||||
"k8s.io/kubernetes/cmd/kubeadm/app/util"
|
||||
volumeutil "k8s.io/kubernetes/pkg/volume/util"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -479,10 +480,11 @@ func TestBuildDaemonSet(t *testing.T) {
|
|||
}
|
||||
defer os.Remove(tempFile)
|
||||
|
||||
podSpec, err := loadPodSpecFromFile(tempFile)
|
||||
pod, err := volumeutil.LoadPodFromFile(tempFile)
|
||||
if err != nil {
|
||||
t.Fatalf("couldn't load the specified Pod")
|
||||
}
|
||||
podSpec := &pod.Spec
|
||||
|
||||
ds := BuildDaemonSet(rt.component, podSpec, GetDefaultMutators())
|
||||
dsBytes, err := util.MarshalToYaml(ds, apps.SchemeGroupVersion)
|
||||
|
@ -554,7 +556,7 @@ spec:
|
|||
}
|
||||
defer os.Remove(tempFile)
|
||||
|
||||
_, err = loadPodSpecFromFile(tempFile)
|
||||
_, err = volumeutil.LoadPodFromFile(tempFile)
|
||||
if (err != nil) != rt.expectError {
|
||||
t.Errorf("failed TestLoadPodSpecFromFile:\nexpected error:\n%t\nsaw:\n%v", rt.expectError, err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue