Skip to content
This repository has been archived by the owner on Sep 24, 2021. It is now read-only.

All the fixes for cluster api v0.1.4 #62

Merged
merged 4 commits into from
Jul 5, 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
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,6 @@ Make sure you have `kubectl`.

`export KUBECONFIG="${HOME}/.kube/kind-config-management"`

1. Install the cluster-api CRDs

`capdctl crds | kubectl apply -f -`

1. Run the capd & capi manager

`capdctl capd -capd-image=<YOUR_REGISTRY>/capd-manager:latest | kubectl apply -f -`

### Create a worker cluster

`kubectl apply -f examples/simple-cluster.yaml`
Expand Down
19 changes: 18 additions & 1 deletion actuators/actuators.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package actuators

import (
"bytes"
"fmt"
"io/ioutil"

Expand Down Expand Up @@ -71,6 +72,22 @@ func kubeconfigToSecret(clusterName, namespace string) (*v1.Secret, error) {
return nil, errors.WithStack(err)
}

allNodes, err := nodes.List(fmt.Sprintf("label=%s=%s", constants.ClusterLabelKey, clusterName))
if err != nil {
return nil, errors.WithStack(err)
}

// This is necessary so the management cluster in a container can talk to another container.
chuckha marked this conversation as resolved.
Show resolved Hide resolved
// They share the same bridged network and the load balancer does respond on 6443 at its docker IP
// however, the *HOST* is listening on some random port (the one returned from the GetLoadBalancerHostAndPort).
lbip, _, err := actions.GetLoadBalancerHostAndPort(allNodes)
lines := bytes.Split(data, []byte("\n"))
Copy link

Choose a reason for hiding this comment

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

Hi @chuckha,

Why not use ScanLines? Even if not, maybe prefer the Golang regex \r?\n?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ScanLines would also work, this was just in my head at the time. If this becomes an issue in the future we can address it later.

Copy link

Choose a reason for hiding this comment

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

Fair, but I also don't think there's a good reason for not switching to stdlib.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't understand what you mean, bytes is in the stdlib

Copy link

Choose a reason for hiding this comment

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

Yeah, sorry, my response was poorly worded. I was referring to how ScanLines uses a more comprehensive matching pattern. If this won't be an issue here because the line endings are guaranteed, then this is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah i see what you mean. Yes, that would definitely be more robust. It will be worth fixing if we need to fix it later as I'm not expecting line endings other than \n at this time. The file we're reading is generated by kubeadm on a unix based system.

Copy link

Choose a reason for hiding this comment

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

FWIW, I know Golang treats \n as a special character. If you use it in a fmt.Sprintf (or a variant), Go will always translate \n to the OS-specific line-ending character(s). I'm not sure if this is the case on scanning though. Either way, as you said, it's a non-issue here.

for i, line := range lines {
if bytes.Contains(line, []byte("https://")) {
lines[i] = []byte(fmt.Sprintf(" server: https://%s:%d", lbip, 6443))
Copy link

Choose a reason for hiding this comment

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

Hi @chuckha,

Should the port always be assumed?

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, the port should always be assumed as 6443. The apiservers will always be listening on their own IP at port 6443 until we allow users to customize support. This will change when support for that feature is added.

}
}

// write it to a secret
return &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -80,7 +97,7 @@ func kubeconfigToSecret(clusterName, namespace string) (*v1.Secret, error) {
},
Data: map[string][]byte{
// TODO pull in constant from cluster api
"value": data,
"value": bytes.Join(lines, []byte("\n")),
},
}, nil
}
49 changes: 7 additions & 42 deletions actuators/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"time"

"github.com/go-logr/logr"
apicorev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"sigs.k8s.io/cluster-api-provider-docker/kind/actions"
Expand Down Expand Up @@ -81,14 +80,9 @@ func (m *Machine) Create(ctx context.Context, c *clusterv1.Cluster, machine *clu
m.Log.Error(err, "Error adding control plane")
return err
}
nodeUID, err := actions.GetNodeRefUID(c.GetName(), controlPlaneNode.Name())
if err != nil {
m.Log.Error(err, "Error getting node reference UID")
return err
}
providerID := providerID(controlPlaneNode.Name())
providerID := actions.ProviderID(controlPlaneNode.Name())
chuckha marked this conversation as resolved.
Show resolved Hide resolved
machine.Spec.ProviderID = &providerID
return m.save(old, machine, getNodeRef(controlPlaneNode.Name(), nodeUID))
return m.save(old, machine)
}

m.Log.Info("Creating a brand new cluster")
Expand All @@ -107,15 +101,10 @@ func (m *Machine) Create(ctx context.Context, c *clusterv1.Cluster, machine *clu
m.Log.Error(err, "Error creating control plane")
return err
}
nodeUID, err := actions.GetNodeRefUID(c.GetName(), controlPlaneNode.Name())
if err != nil {
m.Log.Error(err, "Error getting node reference UID")
return err
}
// set the machine's providerID
providerID := providerID(controlPlaneNode.Name())
providerID := actions.ProviderID(controlPlaneNode.Name())
machine.Spec.ProviderID = &providerID
if err := m.save(old, machine, getNodeRef(controlPlaneNode.Name(), nodeUID)); err != nil {
if err := m.save(old, machine); err != nil {
m.Log.Error(err, "Error setting machine's provider ID")
return err
}
Expand Down Expand Up @@ -144,14 +133,9 @@ func (m *Machine) Create(ctx context.Context, c *clusterv1.Cluster, machine *clu
m.Log.Error(err, "Error creating new worker node")
return err
}
providerID := providerID(worker.Name())
providerID := actions.ProviderID(worker.Name())
machine.Spec.ProviderID = &providerID
nodeUID, err := actions.GetNodeRefUID(c.GetName(), worker.Name())
if err != nil {
m.Log.Error(err, "Error getting node reference ID")
return err
}
return m.save(old, machine, getNodeRef(worker.Name(), nodeUID))
return m.save(old, machine)
}

// Delete returns nil when the machine no longer exists or when a successful delete has happened.
Expand Down Expand Up @@ -201,7 +185,7 @@ func (m *Machine) Exists(ctx context.Context, cluster *clusterv1.Cluster, machin
}

// patches the object and saves the status.
func (m *Machine) save(oldMachine, newMachine *clusterv1.Machine, noderef *apicorev1.ObjectReference) error {
func (m *Machine) save(oldMachine, newMachine *clusterv1.Machine) error {
m.Log.Info("updating machine")
p, err := patch.NewJSONPatch(oldMachine, newMachine)
if err != nil {
Expand All @@ -222,19 +206,9 @@ func (m *Machine) save(oldMachine, newMachine *clusterv1.Machine, noderef *apico
}
m.Log.Info("updated machine")
}
// set the noderef after so we don't try and patch it in during the first update
newMachine.Status.NodeRef = noderef
if _, err := m.ClusterAPI.Machines(oldMachine.Namespace).UpdateStatus(newMachine); err != nil {
m.Log.Error(err, "Error setting node reference")
return err
}
return nil
}

func providerID(name string) string {
return fmt.Sprintf("docker:////%s", name)
}

// CAPIroleToKindRole converts a CAPI role to kind role
// TODO there is a better way to do this.
func CAPIroleToKindRole(CAPIRole string) string {
Expand All @@ -243,12 +217,3 @@ func CAPIroleToKindRole(CAPIRole string) string {
}
return CAPIRole
}

func getNodeRef(name, uid string) *apicorev1.ObjectReference {
return &apicorev1.ObjectReference{
Kind: "Node",
APIVersion: apicorev1.SchemeGroupVersion.String(),
Name: name,
UID: types.UID(uid),
}
}
5 changes: 5 additions & 0 deletions cmd/capd-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package main

import (
"flag"
"fmt"
"time"

Expand All @@ -35,6 +36,9 @@ import (
)

func main() {
flag.Set("v", "0")
chuckha marked this conversation as resolved.
Show resolved Hide resolved
flag.Parse()

cfg, err := config.GetConfig()
if err != nil {
panic(err)
Copy link

Choose a reason for hiding this comment

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

klog.Fatal?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, This is covered in #8

I'll time that out soon depending on if the contributor contributes or not

Expand Down Expand Up @@ -67,6 +71,7 @@ func main() {

machineLogger := logger.Log{}
machineLogger.Logger = klogr.New().WithName("[machine-actuator]")

machineActuator := actuators.Machine{
Core: k8sclientset.CoreV1(),
ClusterAPI: cs.ClusterV1alpha1(),
Expand Down
Loading