Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create kubeconfig for each cluster in reconcile function #412

Merged
merged 2 commits into from
Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gopkg.lock

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

2 changes: 1 addition & 1 deletion cmd/clusterctl/examples/openstack/cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ spec:
kind: "OpenstackProviderSpec"
tags:
- a_cluster_wide_tag

masterIP: <master ip>
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ spec:
allows all traffic to/from machines belonging to that group. In the future,
we could make this more flexible.
type: boolean
masterIP:
description: MasterIP is the ip of the master, if there is Loadbalancer,
should be LB ip and no LB should be floating ip address
type: string
metadata:
type: object
nodeCidr:
Expand Down
1 change: 1 addition & 0 deletions config/rbac/rbac_role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ rules:
resources:
- secrets
verbs:
- create
- get
- list
- watch
4 changes: 4 additions & 0 deletions pkg/apis/openstackproviderconfig/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ type OpenstackClusterProviderSpec struct {

// SAKeyPair is the service account key pair.
SAKeyPair KeyPair `json:"saKeyPair,omitempty"`

// MasterIP is the ip of the master, if there is Loadbalancer, should be LB
// ip and no LB should be floating ip address
MasterIP string `json:"masterIP,omitempty"`
}

// KeyPair is how operators can supply custom keypairs for kubeadm to use.
Expand Down
28 changes: 28 additions & 0 deletions pkg/cloud/openstack/cluster/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
apiv1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
Expand All @@ -17,6 +20,7 @@ import (
"sigs.k8s.io/cluster-api-provider-openstack/pkg/deployer"
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
clientclusterv1 "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset/typed/cluster/v1alpha1"
"sigs.k8s.io/cluster-api/pkg/controller/remote"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/patch"
)
Expand Down Expand Up @@ -110,6 +114,30 @@ func (a *Actuator) Reconcile(cluster *clusterv1.Cluster) error {
return errors.Errorf("failed to reconcile security groups: %v", err)
}

// Store KubeConfig for Cluster API NodeRef controller to use.
kubeConfigSecretName := remote.KubeConfigSecretName(cluster.Name)
if _, err := a.params.KubeClient.CoreV1().Secrets(cluster.Namespace).Get(kubeConfigSecretName, metav1.GetOptions{}); err != nil && apierrors.IsNotFound(err) {
kubeConfig, err := a.Deployer.GetKubeConfig(cluster, nil)
if err != nil {
return errors.Wrapf(err, "failed to get kubeconfig for cluster %q", cluster.Name)
}

kubeConfigSecret := &apiv1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: kubeConfigSecretName,
},
StringData: map[string]string{
"value": kubeConfig,
},
}

if _, err := a.params.KubeClient.CoreV1().Secrets(cluster.Namespace).Create(kubeConfigSecret); err != nil {
return errors.Wrapf(err, "failed to create kubeconfig secret for cluster %q", cluster.Name)
}
} else if err != nil {
return errors.Wrapf(err, "failed to get kubeconfig secret for cluster %q", cluster.Name)
}

return nil
}

Expand Down
15 changes: 12 additions & 3 deletions pkg/deployer/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,18 @@ func (d *Deployer) GetKubeConfig(cluster *clusterv1.Cluster, master *clusterv1.M
return "", errors.New("key not found in status")
}

ip, err := d.GetIP(cluster, master)
if err != nil {
return "", err
var ip string
if master != nil {
ip, err = d.GetIP(cluster, master)
if err != nil {
return "", err
}
} else {
// This case means no master created yet, we need get from cluster info anyway
if len(config.MasterIP) == 0 {
return "", errors.New("MasterIP in cluster spec not set")
}
ip = config.MasterIP
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this would be in the else branch if ManagedAPIServerLoadBalancer is set to false

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, let's work on that patch first then rebase from there

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. I'll rebase mine


server := fmt.Sprintf("https://%s:443", ip)
Expand Down