Skip to content

Commit

Permalink
Use namespace from provider secret
Browse files Browse the repository at this point in the history
  • Loading branch information
stoyanr committed Aug 25, 2020
1 parent 358efa2 commit b7bba4e
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 57 deletions.
3 changes: 0 additions & 3 deletions charts/internal/machine-class/templates/machine-class.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ providerSpec:
{{- if $machineClass.memory }}
memory: "{{ $machineClass.memory }}"
{{ end }}
{{- if $machineClass.namespace }}
namespace: "{{ $machineClass.namespace }}"
{{ end }}
secretRef:
name: "{{ $machineClass.name }}"
namespace: "{{ $.Release.Namespace }}"
Expand Down
3 changes: 1 addition & 2 deletions charts/internal/machine-class/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ machineClasses:
pvcSize: "10Gi"
sourceURL: source-image-url
cpus: "1"
memory: "4096M"
namespace: default
memory: "4096M"
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2
github.com/go-ini/ini v1.36.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0 h1:MP4Eh7ZCb31lleYCFuwm0oe4/YGak+5l1vA2NOE80nA=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-logr/logr v0.1.0 h1:M1Tv3VzNlEHg6uyACnRdtrploV2P7wZqH8BoQMtz0cg=
github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas=
Expand Down Expand Up @@ -196,6 +197,7 @@ github.com/go-openapi/swag v0.19.5 h1:lTz6Ys4CmqqCQmZPBlbQENR1/GucA2bzYTE12Pw4tF
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/go-openapi/validate v0.18.0/go.mod h1:Uh4HdOzKt19xGIGm1qHf/ofbX1YQ4Y+MYsct2VUrAJ4=
github.com/go-openapi/validate v0.19.2/go.mod h1:1tRCw7m3jtI8eNWEEliiAqUIcBztB2KDnRCRMUi7GTA=
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gobuffalo/envy v1.7.0/go.mod h1:n7DRkBerg/aorDM8kbduw5dN3oXGswK5liaSCx4T5NI=
github.com/gobuffalo/flect v0.1.5/go.mod h1:W3K3X9ksuZfir8f/LrfVtWmCDQFfayuylOJ7sz/Fj80=
Expand All @@ -218,6 +220,7 @@ github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d/go.mod h1:SlYgWuQ5
github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls=
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/goji/httpauth v0.0.0-20160601135302-2da839ab0f4d/go.mod h1:nnjvkQ9ptGaCkuDUx6wNykzzlUixGxvkme+H/lnzb+A=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20180513044358-24b0969c4cb7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down Expand Up @@ -275,6 +278,7 @@ github.com/gophercloud/gophercloud v0.7.0/go.mod h1:gmC5oQqMDOMO1t1gq5DquX/yAU80
github.com/gophercloud/utils v0.0.0-20190527093828-25f1b77b8c03/go.mod h1:SZ9FTKibIotDtCrxAU/evccoyu1yhKST6hgBvwTB5Eg=
github.com/gophercloud/utils v0.0.0-20200204043447-9864b6f1f12f/go.mod h1:ehWUbLQJPqS0Ep+CxeD559hsm9pthPXadJNKwZkp43w=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/gregjones/httpcache v0.0.0-20181110185634-c63ab54fda8f/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
Expand Down Expand Up @@ -330,6 +334,7 @@ github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgo
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 h1:T+h1c/A9Gawja4Y9mFVWj2vyii2bbUNDw3kt9VxK2EY=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand Down Expand Up @@ -401,6 +406,7 @@ github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/packethost/packngo v0.0.0-20181217122008-b3b45f1b4979/go.mod h1:otzZQXgoO96RTzDB/Hycg0qZcXZsWJGJRSXbmEIJ+4M=
github.com/pborman/uuid v0.0.0-20170612153648-e790cca94e6c/go.mod h1:VyrYX9gd7irzKovcSS6BIIEwPRkP2Wm2m9ufcdFSJ34=
github.com/pborman/uuid v1.2.0 h1:J7Q5mO4ysT1dv8hyrUGHb9+ooztCXu1D8MY8DZYsu3g=
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys=
Expand All @@ -419,6 +425,7 @@ github.com/prometheus/client_golang v0.9.2 h1:awm861/B8OKDd2I/6o1dy3ra4BamzKhYOi
github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 h1:gQz4mCbXsO+nc9n1hCxHcGA3Zx3Eo+UHZoInFGUIXNM=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M=
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand All @@ -429,6 +436,7 @@ github.com/prometheus/common v0.6.0 h1:kRhiuYSXR3+uv2IbVbZhUxK5zVD/2pp3Gd2PpvPkp
github.com/prometheus/common v0.6.0/go.mod h1:eBmuwkDJBwy6iBfxCBob6t6dR6ENT/y+J+Zk0j9GMYc=
github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a h1:9a8MnZMP0X2nLJdBg+pBmGgkJlSaKC2KaQmTCk1XDtE=
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.2 h1:6LJUbpNm42llc4HRCuvApCSWB/WfhuNo9K98Q9sNGfs=
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
Expand Down Expand Up @@ -785,6 +793,7 @@ k8s.io/cluster-bootstrap v0.17.6/go.mod h1:4UjXhBYavwb5x+XcpbhXvSAbrkVTqTJgbNI0w
k8s.io/code-generator v0.16.8 h1:R3NoYlz4AoTPvwOoDU0dTNV/P9uY3z8H8nLlz9nOo5M=
k8s.io/code-generator v0.16.8/go.mod h1:wFdrXdVi/UC+xIfLi+4l9elsTT/uEF61IfcN2wOLULQ=
k8s.io/component-base v0.0.0-20190918160511-547f6c5d7090/go.mod h1:933PBGtQFJky3TEwYx4aEPZ4IxqhWh3R6DCmzqIn1hA=
k8s.io/component-base v0.16.8 h1:R75NRLguyWm3L+Du7umX6Ed1i4myhE4P8SEnzDQWxIE=
k8s.io/component-base v0.16.8/go.mod h1:Q8UWOWShpP3MZZny4n/15gOncfaaVtc9SbCdkM5MhUE=
k8s.io/component-base v0.17.6/go.mod h1:jgRLWl0B0rOzFNtxQ9E4BphPmDqoMafujdau6AdG2Xo=
k8s.io/component-base v0.18.4 h1:Kr53Fp1iCGNsl9Uv4VcRvLy7YyIqi9oaJOQ7SXtKI98=
Expand All @@ -808,6 +817,7 @@ k8s.io/kube-openapi v0.0.0-20190816220812-743ec37842bf/go.mod h1:1TqjTSzOxsLGIKf
k8s.io/kube-openapi v0.0.0-20191107075043-30be4d16710a/go.mod h1:1TqjTSzOxsLGIKfj0lK8EeCP7K1iUG65v09OM0/WG5E=
k8s.io/kube-openapi v0.0.0-20200410145947-bcb3869e6f29 h1:NeQXVJ2XFSkRoPzRo8AId01ZER+j8oV4SZADT4iBOXQ=
k8s.io/kube-openapi v0.0.0-20200410145947-bcb3869e6f29/go.mod h1:F+5wygcW0wmRTnM3cOgIqGivxkwSWIWT5YdsDbeAOaU=
k8s.io/kubelet v0.16.8 h1:737GeCz3Bu4NxGivTj910vPaL6UYkxaXoOGuMarzCI0=
k8s.io/kubelet v0.16.8/go.mod h1:mzDpnryQg2dlB6V3/WAgb1baIamiICtWpXMFrPOFh6I=
k8s.io/kubelet v0.17.6 h1:PCFzlxfwehcdj0T+1+ypidkBDSJk7EFM1JuH7BrUNos=
k8s.io/kubelet v0.17.6/go.mod h1:H7KZAYjmw/M8LkZq14BfLcvOTOWWocOv8H4dGkkipLM=
Expand Down
11 changes: 0 additions & 11 deletions hack/api-reference/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,17 +280,6 @@ string
<p>MachineTypeName is the name of the machine type, used as a reference to MachineType object</p>
</td>
</tr>
<tr>
<td>
<code>namespace</code></br>
<em>
string
</em>
</td>
<td>
<p>Namespace is the name of the namespace where the machines should be deployed</p>
</td>
</tr>
</tbody>
</table>
<h3 id="kubevirt.provider.extensions.gardener.cloud/v1alpha1.MachineImage">MachineImage
Expand Down
2 changes: 0 additions & 2 deletions pkg/apis/kubevirt/types_cloudprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,4 @@ type MachineImageVersion struct {
type MachineDeploymentConfig struct {
// MachineTypeName is the name of the machine type, used as a reference to MachineType object
MachineTypeName string
// Namespace is the name of the namespace where the machines should be deployed
Namespace string
}
2 changes: 0 additions & 2 deletions pkg/apis/kubevirt/v1alpha1/types_cloudprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,4 @@ type MachineImageVersion struct {
type MachineDeploymentConfig struct {
// MachineTypeName is the name of the machine type, used as a reference to MachineType object
MachineTypeName string `json:"machineTypeName"`
// Namespace is the name of the namespace where the machines should be deployed
Namespace string `json:"namespace"`
}
2 changes: 0 additions & 2 deletions pkg/apis/kubevirt/v1alpha1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 2 additions & 27 deletions pkg/controller/worker/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@ import (
apiskubevirt "github.com/gardener/gardener-extension-provider-kubevirt/pkg/apis/kubevirt"
"github.com/gardener/gardener-extension-provider-kubevirt/pkg/kubevirt"

extensionscontroller "github.com/gardener/gardener/extensions/pkg/controller"
"github.com/gardener/gardener/extensions/pkg/controller/worker"
corev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/gardener/gardener/pkg/client/kubernetes"
machinev1alpha1 "github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"
"k8s.io/apimachinery/pkg/runtime"
)

const KubeconfigSecretKey = "kubeconfig"

// MachineClassKind yields the name of the KubeVirt machine class.
func (w *workerDelegate) MachineClassKind() string {
return "MachineClass"
Expand Down Expand Up @@ -65,30 +62,14 @@ func (w *workerDelegate) GenerateMachineDeployments(ctx context.Context) (worker
return w.machineDeployments, nil
}

func (w *workerDelegate) generateMachineClassSecretData(ctx context.Context) (map[string][]byte, error) {
secret, err := extensionscontroller.GetSecretByReference(ctx, w.Client(), &w.worker.Spec.SecretRef)
if err != nil {
return nil, err
}

kubeconfig, ok := secret.Data[KubeconfigSecretKey]
if !ok {
return nil, fmt.Errorf("missing %q field in secret", KubeconfigSecretKey)
}

return map[string][]byte{
KubeconfigSecretKey: kubeconfig,
}, nil
}

func (w *workerDelegate) generateMachineConfig(ctx context.Context) error {
var (
machineDeployments = worker.MachineDeployments{}
machineClasses []map[string]interface{}
machineImages []apiskubevirt.MachineImage
)

secretData, err := w.generateMachineClassSecretData(ctx)
kubeconfig, err := kubevirt.GetKubeConfig(ctx, w.Client(), w.worker.Spec.SecretRef)
if err != nil {
return err
}
Expand All @@ -103,11 +84,6 @@ func (w *workerDelegate) generateMachineConfig(ctx context.Context) error {
return err
}

machineDeploymentConfig, err := w.getMachineDeploymentConfig(pool.MachineType)
if err != nil {
return err
}

workerPoolHash, err := worker.WorkerPoolHash(pool, w.cluster)
if err != nil {
return err
Expand All @@ -133,14 +109,13 @@ func (w *workerDelegate) generateMachineConfig(ctx context.Context) error {
"sourceURL": imageSourceURL,
"cpus": machineType.CPU,
"memory": machineType.Memory,
"namespace": machineDeploymentConfig.Namespace,
"tags": map[string]string{
"mcm.gardener.cloud/cluster": w.worker.Namespace,
"mcm.gardener.cloud/role": "node",
},
"secret": map[string]interface{}{
"cloudConfig": string(pool.UserData),
"kubeconfig": string(secretData[KubeconfigSecretKey]),
"kubeconfig": string(kubeconfig),
},
})

Expand Down
24 changes: 19 additions & 5 deletions pkg/controller/worker/machines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,13 @@ var _ = Describe("Machines", func() {
machineClassTemplate := map[string]interface{}{
"storageClassName": "standard",
"sourceURL": ubuntuSourceURL,
"namespace": "default",
"tags": map[string]string{
"mcm.gardener.cloud/cluster": namespace,
"mcm.gardener.cloud/role": "node",
},
"secret": map[string]interface{}{
"cloudConfig": "user-data",
"kubeconfig": "YXNkCg==",
"kubeconfig": kubeconfig,
},
}

Expand Down Expand Up @@ -326,12 +325,29 @@ var _ = Describe("Machines", func() {
})
})

const kubeconfig = `apiVersion: v1
kind: Config
current-context: provider
clusters:
- name: provider
cluster:
server: https://provider.example.com
contexts:
- name: provider
context:
cluster: provider
user: admin
users:
- name: admin
user:
token: abc`

func generateKubeVirtSecret(c *mockclient.MockClient) {
c.EXPECT().
Get(context.TODO(), gomock.Any(), gomock.AssignableToTypeOf(&corev1.Secret{})).
DoAndReturn(func(_ context.Context, _ client.ObjectKey, secret *corev1.Secret) error {
secret.Data = map[string][]byte{
KubeconfigSecretKey: []byte("YXNkCg=="),
kubevirt.KubeconfigSecretKey: []byte(kubeconfig),
}
return nil
})
Expand Down Expand Up @@ -361,11 +377,9 @@ func createCluster(cloudProfileName, shootVersion string, images []apiv1alpha1.M
MachineImages: images,
MachineDeploymentConfig: []apiv1alpha1.MachineDeploymentConfig{
{
Namespace: "default",
MachineTypeName: "local-1",
},
{
Namespace: "default",
MachineTypeName: "local-2",
},
},
Expand Down
12 changes: 9 additions & 3 deletions pkg/kubevirt/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ import (
"context"

extensionscontroller "github.com/gardener/gardener/extensions/pkg/controller"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const KubeconfigSecretKey = "kubeconfig"

// GetKubeConfig retrieves the kubeconfig specified by the secret reference.
func GetKubeConfig(ctx context.Context, c client.Client, secretRef corev1.SecretReference) ([]byte, error) {
secret, err := extensionscontroller.GetSecretByReference(ctx, c, &secretRef)
if err != nil {
return []byte(""), err
return []byte(""), errors.Wrapf(err, "could not get secret by reference")
}

return secret.Data["kubeconfig"], nil
kubeconfig, ok := secret.Data[KubeconfigSecretKey]
if !ok {
return nil, errors.Errorf("missing %q field in secret", KubeconfigSecretKey)
}
return kubeconfig, nil
}

0 comments on commit b7bba4e

Please sign in to comment.