Skip to content

Instantly share code, notes, and snippets.

@aojea
Created July 4, 2024 08:32
Show Gist options
  • Save aojea/f94a02dbb12bc3bd1c6d588ea854964f to your computer and use it in GitHub Desktop.
Save aojea/f94a02dbb12bc3bd1c6d588ea854964f to your computer and use it in GitHub Desktop.
Run kube-apiserver and control plane components for test
commit 757999d6b5e3ed7d218313042eb8773d59edb44c
Author: Antonio Ojea <aojea@google.com>
Date: Thu Jul 4 08:30:02 2024
test local-up
diff --git a/test/integration/localup/localup_test.go b/test/integration/localup/localup_test.go
new file mode 100644
index 00000000000..93440417d39
--- /dev/null
+++ b/test/integration/localup/localup_test.go
@@ -0,0 +1,200 @@
+/*
+Copyright 2024 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 localup
+
+import (
+ "context"
+ "fmt"
+ "os"
+ "testing"
+ "time"
+
+ v1 "k8s.io/api/core/v1"
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+ clientset "k8s.io/client-go/kubernetes"
+ "k8s.io/client-go/rest"
+ "k8s.io/client-go/tools/clientcmd"
+ clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
+ cloudproviderapi "k8s.io/cloud-provider/api"
+ kubeapiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing"
+ "k8s.io/kubernetes/test/integration/framework"
+)
+
+func Test_Local_Up(t *testing.T) {
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+
+ // start kube-apiserver
+ args := framework.DefaultTestServerFlags()
+
+ server := kubeapiservertesting.StartTestServerOrDie(t, nil, args, framework.SharedEtcd())
+ defer server.TearDownFn()
+
+ client := clientset.NewForConfigOrDie(server.ClientConfig)
+
+ ns := framework.CreateNamespaceOrDie(client, "test", t)
+ defer framework.DeleteNamespaceOrDie(client, ns, t)
+
+ kubeconfig := createKubeconfigFileForRestConfig(server.ClientConfig)
+ // nolint:errcheck // Ignore the error trying to delete the kubeconfig file used for the test
+ defer os.Remove(kubeconfig)
+
+ // start kube-controller-manager
+ /*
+
+ args = []string{
+ "--kubeconfig=" + kubeconfig,
+ "--cloud-provider=external",
+ "--cidr-allocator-type=" + string(ipam.RangeAllocatorType),
+ "--configure-cloud-routes=false",
+ }
+
+ kcm := kcmservertesting.StartTestServerOrDie(ctx, args)
+ defer kcm.TearDownFn()
+ */
+
+ // start kube-scheduler
+ /*
+ config := &config.KubeSchedulerConfiguration{}
+ _, informerFactory := util.StartScheduler(ctx, client, server.ClientConfig, config, nil)
+ informerFactory.Start(ctx.Done())
+ informerFactory.WaitForCacheSync(ctx.Done())
+ */
+
+ // start cloud-controller-manager
+ /*
+ args = []string{
+ "--kubeconfig=" + kubeconfig,
+ "--cloud-provider=fakeCloud",
+ "--cidr-allocator-type=" + string(ipam.RangeAllocatorType),
+ "--configure-cloud-routes=false",
+ }
+
+ fakeCloud := &fakecloud.Cloud{
+ Zone: cloudprovider.Zone{
+ FailureDomain: "zone-0",
+ Region: "region-1",
+ },
+ EnableInstancesV2: true,
+ ExistsByProviderID: true,
+ ProviderID: map[types.NodeName]string{
+ types.NodeName("node0"): "12345",
+ },
+ InstanceTypes: map[types.NodeName]string{
+ types.NodeName("node0"): "t1.micro",
+ },
+ ExtID: map[types.NodeName]string{
+ types.NodeName("node0"): "12345",
+ },
+ Addresses: []v1.NodeAddress{
+ {
+ Type: v1.NodeHostName,
+ Address: "node0.cloud.internal",
+ },
+ {
+ Type: v1.NodeInternalIP,
+ Address: "10.0.0.1",
+ },
+ {
+ Type: v1.NodeExternalIP,
+ Address: "132.143.154.163",
+ },
+ },
+ ErrByProviderID: nil,
+ Err: nil,
+ }
+
+ // register fake GCE cloud provider
+ cloudprovider.RegisterCloudProvider(
+ "fakeCloud",
+ func(config io.Reader) (cloudprovider.Interface, error) {
+ return fakeCloud, nil
+ })
+
+ ccm := ccmservertesting.StartTestServerOrDie(ctx, args)
+ defer ccm.TearDownFn()
+
+ */
+
+ // Create fake node
+ _, err := client.CoreV1().Nodes().Create(ctx, makeNode("node0"), metav1.CreateOptions{})
+ if err != nil {
+ t.Fatalf("Failed to create Node %v", err)
+ }
+
+ for {
+ fmt.Printf("-------> Kubeconfig file created on: %s\n", kubeconfig)
+ time.Sleep(5 * time.Second)
+ }
+}
+
+// sigs.k8s.io/controller-runtime/pkg/envtest
+func createKubeconfigFileForRestConfig(restConfig *rest.Config) string {
+ clusters := make(map[string]*clientcmdapi.Cluster)
+ clusters["default-cluster"] = &clientcmdapi.Cluster{
+ Server: restConfig.Host,
+ TLSServerName: restConfig.ServerName,
+ CertificateAuthorityData: restConfig.CAData,
+ }
+ contexts := make(map[string]*clientcmdapi.Context)
+ contexts["default-context"] = &clientcmdapi.Context{
+ Cluster: "default-cluster",
+ AuthInfo: "default-user",
+ }
+ authinfos := make(map[string]*clientcmdapi.AuthInfo)
+ authinfos["default-user"] = &clientcmdapi.AuthInfo{
+ ClientCertificateData: restConfig.CertData,
+ ClientKeyData: restConfig.KeyData,
+ Token: restConfig.BearerToken,
+ }
+ clientConfig := clientcmdapi.Config{
+ Kind: "Config",
+ APIVersion: "v1",
+ Clusters: clusters,
+ Contexts: contexts,
+ CurrentContext: "default-context",
+ AuthInfos: authinfos,
+ }
+ kubeConfigFile, _ := os.CreateTemp("", "kubeconfig")
+ _ = clientcmd.WriteToFile(clientConfig, kubeConfigFile.Name())
+ return kubeConfigFile.Name()
+}
+
+func makeNode(name string) *v1.Node {
+ return &v1.Node{
+ ObjectMeta: metav1.ObjectMeta{
+ Name: name,
+ },
+ Spec: v1.NodeSpec{
+ Taints: []v1.Taint{{
+ Key: cloudproviderapi.TaintExternalCloudProvider,
+ Value: "true",
+ Effect: v1.TaintEffectNoSchedule,
+ }},
+ Unschedulable: false,
+ },
+ Status: v1.NodeStatus{
+ Conditions: []v1.NodeCondition{
+ {
+ Type: v1.NodeReady,
+ Status: v1.ConditionUnknown,
+ LastHeartbeatTime: metav1.Time{Time: time.Now()},
+ },
+ },
+ },
+ }
+}
diff --git a/test/integration/localup/main_test.go b/test/integration/localup/main_test.go
new file mode 100644
index 00000000000..d19a9b71838
--- /dev/null
+++ b/test/integration/localup/main_test.go
@@ -0,0 +1,27 @@
+/*
+Copyright 2024 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 localup
+
+import (
+ "testing"
+
+ "k8s.io/kubernetes/test/integration/framework"
+)
+
+func TestMain(m *testing.M) {
+ framework.EtcdMain(m.Run)
+}
@aojea
Copy link
Author

aojea commented Jul 4, 2024

go test -run ^Test_Local_Up$ k8s.io/kubernetes/test/integration/localup -v -count 1 -timeout 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment