func fakeUnjoinHostFactory(name string) cmdutil.Factory { urlPrefix := "/api/v1/namespaces/federation-system/secrets/" f, tf, codec, _ := cmdtesting.NewAPIFactory() ns := dynamic.ContentConfig().NegotiatedSerializer tf.ClientConfig = kubefedtesting.DefaultClientConfig() tf.Client = &fake.RESTClient{ NegotiatedSerializer: ns, Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case strings.HasPrefix(p, urlPrefix) && m == http.MethodDelete: got := strings.TrimPrefix(p, urlPrefix) if got != name { return nil, errors.NewNotFound(api.Resource("secrets"), got) } status := unversioned.Status{ Status: "Success", } return &http.Response{StatusCode: http.StatusOK, Header: kubefedtesting.DefaultHeader(), Body: kubefedtesting.ObjBody(codec, &status)}, nil default: return nil, fmt.Errorf("unexpected request: %#v\n%#v", req.URL, req) } }), } return f }
func testUnjoinFederationFactory(name, server, secret string) cmdutil.Factory { urlPrefix := "/clusters/" cluster := fakeCluster(name, name, server) if secret != "" { cluster.Spec.SecretRef.Name = secret } f, tf, _, _ := cmdtesting.NewAPIFactory() codec := testapi.Federation.Codec() tf.ClientConfig = kubefedtesting.DefaultClientConfig() ns := testapi.Federation.NegotiatedSerializer() tf.Client = &fake.RESTClient{ NegotiatedSerializer: ns, GroupName: "federation", Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case strings.HasPrefix(p, urlPrefix): got := strings.TrimPrefix(p, urlPrefix) if got != name { return nil, errors.NewNotFound(federationapi.Resource("clusters"), got) } switch m { case http.MethodGet: return &http.Response{StatusCode: http.StatusOK, Header: kubefedtesting.DefaultHeader(), Body: kubefedtesting.ObjBody(codec, &cluster)}, nil case http.MethodDelete: status := unversioned.Status{ Status: "Success", } return &http.Response{StatusCode: http.StatusOK, Header: kubefedtesting.DefaultHeader(), Body: kubefedtesting.ObjBody(codec, &status)}, nil default: return nil, fmt.Errorf("unexpected method: %#v\n%#v", req.URL, req) } default: return nil, fmt.Errorf("unexpected request: %#v\n%#v", req.URL, req) } }), } tf.Namespace = "test" return f }
func fakeInitHostFactory(federationName, namespaceName, ip, dnsZoneName, image, dnsProvider string) (cmdutil.Factory, error) { svcName := federationName + "-apiserver" svcUrlPrefix := "/api/v1/namespaces/federation-system/services" credSecretName := svcName + "-credentials" cmKubeconfigSecretName := federationName + "-controller-manager-kubeconfig" capacity, err := resource.ParseQuantity("10Gi") if err != nil { return nil, err } pvcName := svcName + "-etcd-claim" replicas := int32(1) namespace := v1.Namespace{ TypeMeta: unversioned.TypeMeta{ Kind: "Namespace", APIVersion: testapi.Default.GroupVersion().String(), }, ObjectMeta: v1.ObjectMeta{ Name: namespaceName, }, } svc := v1.Service{ TypeMeta: unversioned.TypeMeta{ Kind: "Service", APIVersion: testapi.Default.GroupVersion().String(), }, ObjectMeta: v1.ObjectMeta{ Namespace: namespaceName, Name: svcName, Labels: componentLabel, }, Spec: v1.ServiceSpec{ Type: v1.ServiceTypeLoadBalancer, Selector: apiserverSvcSelector, Ports: []v1.ServicePort{ { Name: "https", Protocol: "TCP", Port: 443, TargetPort: intstr.FromInt(443), }, }, }, } svcWithLB := svc svcWithLB.Status = v1.ServiceStatus{ LoadBalancer: v1.LoadBalancerStatus{ Ingress: []v1.LoadBalancerIngress{ { IP: ip, }, }, }, } credSecret := v1.Secret{ TypeMeta: unversioned.TypeMeta{ Kind: "Secret", APIVersion: testapi.Default.GroupVersion().String(), }, ObjectMeta: v1.ObjectMeta{ Name: credSecretName, Namespace: namespaceName, }, Data: nil, } cmKubeconfigSecret := v1.Secret{ TypeMeta: unversioned.TypeMeta{ Kind: "Secret", APIVersion: testapi.Default.GroupVersion().String(), }, ObjectMeta: v1.ObjectMeta{ Name: cmKubeconfigSecretName, Namespace: namespaceName, }, Data: nil, } pvc := v1.PersistentVolumeClaim{ TypeMeta: unversioned.TypeMeta{ Kind: "PersistentVolumeClaim", APIVersion: testapi.Default.GroupVersion().String(), }, ObjectMeta: v1.ObjectMeta{ Name: pvcName, Namespace: namespaceName, Labels: componentLabel, Annotations: map[string]string{ "volume.alpha.kubernetes.io/storage-class": "yes", }, }, Spec: v1.PersistentVolumeClaimSpec{ AccessModes: []v1.PersistentVolumeAccessMode{ v1.ReadWriteOnce, }, Resources: v1.ResourceRequirements{ Requests: v1.ResourceList{ v1.ResourceStorage: capacity, }, }, }, } apiserver := v1beta1.Deployment{ TypeMeta: unversioned.TypeMeta{ Kind: "Deployment", APIVersion: testapi.Extensions.GroupVersion().String(), }, ObjectMeta: v1.ObjectMeta{ Name: svcName, Namespace: namespaceName, Labels: componentLabel, }, Spec: v1beta1.DeploymentSpec{ Replicas: &replicas, Selector: nil, Template: v1.PodTemplateSpec{ ObjectMeta: v1.ObjectMeta{ Name: svcName, Labels: apiserverPodLabels, }, Spec: v1.PodSpec{ Containers: []v1.Container{ { Name: "apiserver", Image: image, Command: []string{ "/hyperkube", "federation-apiserver", "--bind-address=0.0.0.0", "--etcd-servers=http://localhost:2379", "--service-cluster-ip-range=10.0.0.0/16", "--secure-port=443", "--client-ca-file=/etc/federation/apiserver/ca.crt", "--tls-cert-file=/etc/federation/apiserver/server.crt", "--tls-private-key-file=/etc/federation/apiserver/server.key", "--advertise-address=" + ip, }, Ports: []v1.ContainerPort{ { Name: "https", ContainerPort: 443, }, { Name: "local", ContainerPort: 8080, }, }, VolumeMounts: []v1.VolumeMount{ { Name: credSecretName, MountPath: "/etc/federation/apiserver", ReadOnly: true, }, }, }, { Name: "etcd", Image: "quay.io/coreos/etcd:v2.3.3", Command: []string{ "/etcd", "--data-dir", "/var/etcd/data", }, VolumeMounts: []v1.VolumeMount{ { Name: "etcddata", MountPath: "/var/etcd", }, }, }, }, Volumes: []v1.Volume{ { Name: credSecretName, VolumeSource: v1.VolumeSource{ Secret: &v1.SecretVolumeSource{ SecretName: credSecretName, }, }, }, { Name: "etcddata", VolumeSource: v1.VolumeSource{ PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{ ClaimName: pvcName, }, }, }, }, }, }, }, } cmName := federationName + "-controller-manager" cm := v1beta1.Deployment{ TypeMeta: unversioned.TypeMeta{ Kind: "Deployment", APIVersion: testapi.Extensions.GroupVersion().String(), }, ObjectMeta: v1.ObjectMeta{ Name: cmName, Namespace: namespaceName, Labels: componentLabel, }, Spec: v1beta1.DeploymentSpec{ Replicas: &replicas, Selector: nil, Template: v1.PodTemplateSpec{ ObjectMeta: v1.ObjectMeta{ Name: cmName, Labels: controllerManagerPodLabels, }, Spec: v1.PodSpec{ Containers: []v1.Container{ { Name: "controller-manager", Image: image, Command: []string{ "/hyperkube", "federation-controller-manager", "--master=https://federation-apiserver", "--kubeconfig=/etc/federation/controller-manager/kubeconfig", fmt.Sprintf("--dns-provider=%s", dnsProvider), "--dns-provider-config=", fmt.Sprintf("--federation-name=%s", federationName), fmt.Sprintf("--zone-name=%s", dnsZoneName), }, VolumeMounts: []v1.VolumeMount{ { Name: cmKubeconfigSecretName, MountPath: "/etc/federation/controller-manager", ReadOnly: true, }, }, Env: []v1.EnvVar{ { Name: "POD_NAMESPACE", ValueFrom: &v1.EnvVarSource{ FieldRef: &v1.ObjectFieldSelector{ FieldPath: "metadata.namespace", }, }, }, }, }, }, Volumes: []v1.Volume{ { Name: cmKubeconfigSecretName, VolumeSource: v1.VolumeSource{ Secret: &v1.SecretVolumeSource{ SecretName: cmKubeconfigSecretName, }, }, }, }, }, }, }, } f, tf, codec, _ := cmdtesting.NewAPIFactory() extCodec := testapi.Extensions.Codec() ns := dynamic.ContentConfig().NegotiatedSerializer tf.ClientConfig = kubefedtesting.DefaultClientConfig() tf.Client = &fake.RESTClient{ NegotiatedSerializer: ns, Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/api/v1/namespaces" && m == http.MethodPost: body, err := ioutil.ReadAll(req.Body) if err != nil { return nil, err } var got v1.Namespace _, _, err = codec.Decode(body, nil, &got) if err != nil { return nil, err } if !api.Semantic.DeepEqual(got, namespace) { return nil, fmt.Errorf("Unexpected namespace object\n\tDiff: %s", diff.ObjectGoPrintDiff(got, namespace)) } return &http.Response{StatusCode: http.StatusCreated, Header: kubefedtesting.DefaultHeader(), Body: kubefedtesting.ObjBody(codec, &namespace)}, nil case p == svcUrlPrefix && m == http.MethodPost: body, err := ioutil.ReadAll(req.Body) if err != nil { return nil, err } var got v1.Service _, _, err = codec.Decode(body, nil, &got) if err != nil { return nil, err } if !api.Semantic.DeepEqual(got, svc) { return nil, fmt.Errorf("Unexpected service object\n\tDiff: %s", diff.ObjectGoPrintDiff(got, svc)) } return &http.Response{StatusCode: http.StatusCreated, Header: kubefedtesting.DefaultHeader(), Body: kubefedtesting.ObjBody(codec, &svc)}, nil case strings.HasPrefix(p, svcUrlPrefix) && m == http.MethodGet: got := strings.TrimPrefix(p, svcUrlPrefix+"/") if got != svcName { return nil, errors.NewNotFound(api.Resource("services"), got) } return &http.Response{StatusCode: http.StatusOK, Header: kubefedtesting.DefaultHeader(), Body: kubefedtesting.ObjBody(codec, &svcWithLB)}, nil case p == "/api/v1/namespaces/federation-system/secrets" && m == http.MethodPost: body, err := ioutil.ReadAll(req.Body) if err != nil { return nil, err } var got, want v1.Secret _, _, err = codec.Decode(body, nil, &got) if err != nil { return nil, err } // Obtained secret contains generated data which cannot // be compared, so we just nullify the generated part // and compare the rest of the secret. The generated // parts are tested in other tests. got.Data = nil switch got.Name { case credSecretName: want = credSecret case cmKubeconfigSecretName: want = cmKubeconfigSecret } if !api.Semantic.DeepEqual(got, want) { return nil, fmt.Errorf("Unexpected secret object\n\tDiff: %s", diff.ObjectGoPrintDiff(got, want)) } return &http.Response{StatusCode: http.StatusCreated, Header: kubefedtesting.DefaultHeader(), Body: kubefedtesting.ObjBody(codec, &want)}, nil case p == "/api/v1/namespaces/federation-system/persistentvolumeclaims" && m == http.MethodPost: body, err := ioutil.ReadAll(req.Body) if err != nil { return nil, err } var got v1.PersistentVolumeClaim _, _, err = codec.Decode(body, nil, &got) if err != nil { return nil, err } if !api.Semantic.DeepEqual(got, pvc) { return nil, fmt.Errorf("Unexpected PVC object\n\tDiff: %s", diff.ObjectGoPrintDiff(got, pvc)) } return &http.Response{StatusCode: http.StatusCreated, Header: kubefedtesting.DefaultHeader(), Body: kubefedtesting.ObjBody(codec, &pvc)}, nil case p == "/apis/extensions/v1beta1/namespaces/federation-system/deployments" && m == http.MethodPost: body, err := ioutil.ReadAll(req.Body) if err != nil { return nil, err } var got, want v1beta1.Deployment _, _, err = codec.Decode(body, nil, &got) if err != nil { return nil, err } switch got.Name { case svcName: want = apiserver case cmName: want = cm } if !api.Semantic.DeepEqual(got, want) { return nil, fmt.Errorf("Unexpected deployment object\n\tDiff: %s", diff.ObjectGoPrintDiff(got, want)) } return &http.Response{StatusCode: http.StatusCreated, Header: kubefedtesting.DefaultHeader(), Body: kubefedtesting.ObjBody(extCodec, &want)}, nil default: return nil, fmt.Errorf("unexpected request: %#v\n%#v", req.URL, req) } }), } return f, nil }
func fakeJoinHostFactory(clusterName, clusterCtx, secretName, server, token string) (cmdutil.Factory, error) { if clusterCtx == "" { clusterCtx = clusterName } if secretName == "" { secretName = clusterName } kubeconfig := clientcmdapi.Config{ Clusters: map[string]*clientcmdapi.Cluster{ clusterCtx: { Server: server, }, }, AuthInfos: map[string]*clientcmdapi.AuthInfo{ clusterCtx: { Token: token, }, }, Contexts: map[string]*clientcmdapi.Context{ clusterCtx: { Cluster: clusterCtx, AuthInfo: clusterCtx, }, }, CurrentContext: clusterCtx, } configBytes, err := clientcmd.Write(kubeconfig) if err != nil { return nil, err } secretObject := v1.Secret{ TypeMeta: unversioned.TypeMeta{ Kind: "Secret", APIVersion: "v1", }, ObjectMeta: v1.ObjectMeta{ Name: secretName, Namespace: util.DefaultFederationSystemNamespace, }, Data: map[string][]byte{ "kubeconfig": configBytes, }, } f, tf, codec, _ := cmdtesting.NewAPIFactory() ns := dynamic.ContentConfig().NegotiatedSerializer tf.ClientConfig = kubefedtesting.DefaultClientConfig() tf.Client = &fake.RESTClient{ NegotiatedSerializer: ns, Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/api/v1/namespaces/federation-system/secrets" && m == http.MethodPost: body, err := ioutil.ReadAll(req.Body) if err != nil { return nil, err } var got v1.Secret _, _, err = codec.Decode(body, nil, &got) if err != nil { return nil, err } if !api.Semantic.DeepEqual(got, secretObject) { return nil, fmt.Errorf("Unexpected secret object\n\tDiff: %s", diff.ObjectGoPrintDiff(got, secretObject)) } return &http.Response{StatusCode: http.StatusCreated, Header: kubefedtesting.DefaultHeader(), Body: kubefedtesting.ObjBody(codec, &secretObject)}, nil default: return nil, fmt.Errorf("unexpected request: %#v\n%#v", req.URL, req) } }), } return f, nil }