Skip to content

Commit

Permalink
Populate machineset status
Browse files Browse the repository at this point in the history
This PR was split off from PR #165 to make it a smaller PR.
There are edge cases that will be implemented in a separate PR.
  • Loading branch information
k4leung4 committed May 16, 2018
1 parent c0ffba9 commit b2ad52a
Show file tree
Hide file tree
Showing 4 changed files with 424 additions and 21 deletions.
53 changes: 37 additions & 16 deletions pkg/controller/machineset/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,36 @@ package machineset

import (
"fmt"

"github.com/golang/glog"
"github.com/kubernetes-incubator/apiserver-builder/pkg/builders"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"

"sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
machineclientset "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset"
clusterapiclientset "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset"
listers "sigs.k8s.io/cluster-api/pkg/client/listers_generated/cluster/v1alpha1"
"sigs.k8s.io/cluster-api/pkg/controller/sharedinformers"
)

const (
// The number of times we retry updating a ReplicaSet's status.
statusUpdateRetries = 1
)

// controllerKind contains the schema.GroupVersionKind for this controller type.
var controllerKind = v1alpha1.SchemeGroupVersion.WithKind("MachineSet")

// +controller:group=cluster,version=v1alpha1,kind=MachineSet,resource=machinesets
type MachineSetControllerImpl struct {
builders.DefaultControllerFns

// machineClient a client that knows how to consume Machine resources
machineClient machineclientset.Interface
// kubernetesClient a client that knows how to consume Node resources
kubernetesClient kubernetes.Interface

// clusterAPIClient a client that knows how to consume Cluster API resources
clusterAPIClient clusterapiclientset.Interface

// machineSetsLister indexes properties about MachineSet
machineSetsLister listers.MachineSetLister
Expand All @@ -48,13 +59,15 @@ type MachineSetControllerImpl struct {
// Init initializes the controller and is called by the generated code
// Register watches for additional resource types here.
func (c *MachineSetControllerImpl) Init(arguments sharedinformers.ControllerInitArguments) {
c.kubernetesClient = arguments.GetSharedInformers().KubernetesClientSet

c.machineSetsLister = arguments.GetSharedInformers().Factory.Cluster().V1alpha1().MachineSets().Lister()
c.machineLister = arguments.GetSharedInformers().Factory.Cluster().V1alpha1().Machines().Lister()

var err error
c.machineClient, err = machineclientset.NewForConfig(arguments.GetRestConfig())
c.clusterAPIClient, err = clusterapiclientset.NewForConfig(arguments.GetRestConfig())
if err != nil {
glog.Fatalf("error building clientset for machineClient: %v", err)
glog.Fatalf("error building clientset for clusterAPIClient: %v", err)
}
}

Expand All @@ -68,7 +81,18 @@ func (c *MachineSetControllerImpl) Reconcile(machineSet *v1alpha1.MachineSet) er
return err
}

return c.syncReplicas(machineSet, filteredMachines)
syncErr := c.syncReplicas(machineSet, filteredMachines)

ms := machineSet.DeepCopy()
newStatus := c.calculateStatus(ms, filteredMachines)

// Always updates status as machines come up or die.
_, err = updateMachineSetStatus(c.clusterAPIClient.ClusterV1alpha1().MachineSets(machineSet.Namespace), machineSet, newStatus)
if err != nil {
return fmt.Errorf("failed to update machine set status, %v", err)
}

return syncErr
}

func (c *MachineSetControllerImpl) Get(namespace, name string) (*v1alpha1.MachineSet, error) {
Expand All @@ -93,11 +117,8 @@ func (c *MachineSetControllerImpl) syncReplicas(machineSet *v1alpha1.MachineSet,
diff *= -1
for i := 0; i < diff; i++ {
glog.V(2).Infof("creating a machine ( spec.replicas(%d) > currentMachineCount(%d) )", desiredReplicas, currentMachineCount)
machine, err := c.createMachine(machineSet)
if err != nil {
return err
}
_, err = c.machineClient.ClusterV1alpha1().Machines(machineSet.Namespace).Create(machine)
machine := c.createMachine(machineSet)
_, err := c.clusterAPIClient.ClusterV1alpha1().Machines(machineSet.Namespace).Create(machine)
if err != nil {
glog.Errorf("unable to create a machine = %s, due to %v", machine.Name, err)
result = err
Expand All @@ -109,7 +130,7 @@ func (c *MachineSetControllerImpl) syncReplicas(machineSet *v1alpha1.MachineSet,
// TODO: Define machines deletion policies.
// see: https://github.com/kubernetes/kube-deploy/issues/625
machineToDelete := machines[i]
err := c.machineClient.ClusterV1alpha1().Machines(machineSet.Namespace).Delete(machineToDelete.Name, &metav1.DeleteOptions{})
err := c.clusterAPIClient.ClusterV1alpha1().Machines(machineSet.Namespace).Delete(machineToDelete.Name, &metav1.DeleteOptions{})
if err != nil {
glog.Errorf("unable to delete a machine = %s, due to %v", machineToDelete.Name, err)
result = err
Expand All @@ -122,7 +143,7 @@ func (c *MachineSetControllerImpl) syncReplicas(machineSet *v1alpha1.MachineSet,

// createMachine creates a machine resource.
// the name of the newly created resource is going to be created by the API server, we set the generateName field
func (c *MachineSetControllerImpl) createMachine(machineSet *v1alpha1.MachineSet) (*v1alpha1.Machine, error) {
func (c *MachineSetControllerImpl) createMachine(machineSet *v1alpha1.MachineSet) *v1alpha1.Machine {
gv := v1alpha1.SchemeGroupVersion
machine := &v1alpha1.Machine{
TypeMeta: metav1.TypeMeta{
Expand All @@ -133,9 +154,9 @@ func (c *MachineSetControllerImpl) createMachine(machineSet *v1alpha1.MachineSet
Spec: machineSet.Spec.Template.Spec,
}
machine.ObjectMeta.GenerateName = fmt.Sprintf("%s-", machineSet.Name)
machine.ObjectMeta.OwnerReferences = []metav1.OwnerReference{*metav1.NewControllerRef(machineSet, controllerKind),}
machine.ObjectMeta.OwnerReferences = []metav1.OwnerReference{*metav1.NewControllerRef(machineSet, controllerKind)}

return machine, nil
return machine
}

// getMachines returns a list of machines that match on machineSet.Spec.Selector
Expand Down Expand Up @@ -182,7 +203,7 @@ func (c *MachineSetControllerImpl) adoptOrphan(machineSet *v1alpha1.MachineSet,
newRef := *metav1.NewControllerRef(machineSet, controllerKind)
ownerRefs = append(ownerRefs, newRef)
machine.ObjectMeta.SetOwnerReferences(ownerRefs)
if _, err := c.machineClient.ClusterV1alpha1().Machines(machineSet.Namespace).Update(machine); err != nil {
if _, err := c.clusterAPIClient.ClusterV1alpha1().Machines(machineSet.Namespace).Update(machine); err != nil {
glog.Warningf("Failed to update machine owner reference. %v", err)
}
}
23 changes: 18 additions & 5 deletions pkg/controller/machineset/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,13 @@ func TestMachineSetControllerReconcileHandler(t *testing.T) {
if err != nil {
t.Fatal(err)
}
rObjects = append(rObjects, amachineset)
}
fakeClient := fake.NewSimpleClientset(rObjects...)
machineLister := v1alpha1listers.NewMachineLister(machinesIndexer)
machineSetLister := v1alpha1listers.NewMachineSetLister(machineSetIndexer)
target := &MachineSetControllerImpl{}
target.machineClient = fakeClient
target.clusterAPIClient = fakeClient
target.machineSetsLister = machineSetLister
target.machineLister = machineLister

Expand All @@ -171,6 +172,7 @@ func TestMachineSetControllerReconcileHandler(t *testing.T) {

// validate
actions := fakeClient.Actions()
actions = getFilteredActions(actions, "machines")
if len(actions) != len(test.expectedActions) {
t.Fatalf("unexpected actions: %v, expected %d actions got %d", actions, len(test.expectedActions), len(actions))
}
Expand Down Expand Up @@ -222,15 +224,16 @@ func createMachineSet(replicas int, machineSetName string, machineName string, n
Namespace: namespace,
},
Spec: v1alpha1.MachineSetSpec{
Replicas: &replicasInt32,
Selector:metav1.LabelSelector{
MatchLabels: map[string]string{labelKey:"strongMachine"},
Replicas: &replicasInt32,
MinReadySeconds: 600,
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{labelKey: "strongMachine"},
},
Template: v1alpha1.MachineTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: machineName,
Namespace: namespace,
Labels: map[string]string{labelKey:"strongMachine"},
Labels: map[string]string{labelKey: "strongMachine"},
},
Spec: v1alpha1.MachineSpec{
ProviderConfig: v1alpha1.ProviderConfig{
Expand Down Expand Up @@ -298,3 +301,13 @@ func setNonControllerRef(m *v1alpha1.Machine) *v1alpha1.Machine {
m.ObjectMeta.OwnerReferences[0].Controller = &controller
return m
}

func getFilteredActions(actions []clienttesting.Action, resource string) []clienttesting.Action {
var filteredActions []clienttesting.Action
for _, action := range actions {
if action.GetResource().Resource == resource {
filteredActions = append(filteredActions, action)
}
}
return filteredActions
}
165 changes: 165 additions & 0 deletions pkg/controller/machineset/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
Copyright 2018 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 machineset

import (
"fmt"
"time"

"github.com/golang/glog"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"

"sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
machinesetclientset "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset/typed/cluster/v1alpha1"
)

func (c *MachineSetControllerImpl) calculateStatus(ms *v1alpha1.MachineSet, filteredMachines []*v1alpha1.Machine) v1alpha1.MachineSetStatus {
newStatus := ms.Status
// Count the number of machines that have labels matching the labels of the machine
// template of the replica set, the matching machines may have more
// labels than are in the template. Because the label of machineTemplateSpec is
// a superset of the selector of the replica set, so the possible
// matching machines must be part of the filteredMachines.
fullyLabeledReplicasCount := 0
readyReplicasCount := 0
availableReplicasCount := 0
templateLabel := labels.Set(ms.Spec.Template.Labels).AsSelectorPreValidated()
for _, machine := range filteredMachines {
if templateLabel.Matches(labels.Set(machine.Labels)) {
fullyLabeledReplicasCount++
}
node, err := c.getMachineNode(machine)
if err != nil {
glog.Warningf("Unable to get node for machine %v, %v", machine.Name, err)
continue
}
if isNodeReady(node) {
readyReplicasCount++
if isNodeAvailable(node, ms.Spec.MinReadySeconds, metav1.Now()) {
availableReplicasCount++
}
}
}

newStatus.Replicas = int32(len(filteredMachines))
newStatus.FullyLabeledReplicas = int32(fullyLabeledReplicasCount)
newStatus.ReadyReplicas = int32(readyReplicasCount)
newStatus.AvailableReplicas = int32(availableReplicasCount)
return newStatus
}

// updateMachineSetStatus attempts to update the Status.Replicas of the given MachineSet, with a single GET/PUT retry.
func updateMachineSetStatus(c machinesetclientset.MachineSetInterface, ms *v1alpha1.MachineSet, newStatus v1alpha1.MachineSetStatus) (*v1alpha1.MachineSet, error) {
// This is the steady state. It happens when the MachineSet doesn't have any expectations, since
// we do a periodic relist every 30s. If the generations differ but the replicas are
// the same, a caller might've resized to the same replica count.
if ms.Status.Replicas == newStatus.Replicas &&
ms.Status.FullyLabeledReplicas == newStatus.FullyLabeledReplicas &&
ms.Status.ReadyReplicas == newStatus.ReadyReplicas &&
ms.Status.AvailableReplicas == newStatus.AvailableReplicas &&
ms.Generation == ms.Status.ObservedGeneration {
return ms, nil
}

// Save the generation number we acted on, otherwise we might wrongfully indicate
// that we've seen a spec update when we retry.
// TODO: This can clobber an update if we allow multiple agents to write to the
// same status.
newStatus.ObservedGeneration = ms.Generation

var getErr, updateErr error
var updatedMS *v1alpha1.MachineSet
for i := 0; ; i++ {
glog.V(4).Infof(fmt.Sprintf("Updating status for %v: %s/%s, ", ms.Kind, ms.Namespace, ms.Name) +
fmt.Sprintf("replicas %d->%d (need %d), ", ms.Status.Replicas, newStatus.Replicas, *(ms.Spec.Replicas)) +
fmt.Sprintf("fullyLabeledReplicas %d->%d, ", ms.Status.FullyLabeledReplicas, newStatus.FullyLabeledReplicas) +
fmt.Sprintf("readyReplicas %d->%d, ", ms.Status.ReadyReplicas, newStatus.ReadyReplicas) +
fmt.Sprintf("availableReplicas %d->%d, ", ms.Status.AvailableReplicas, newStatus.AvailableReplicas) +
fmt.Sprintf("sequence No: %v->%v", ms.Status.ObservedGeneration, newStatus.ObservedGeneration))

ms.Status = newStatus
updatedMS, updateErr = c.UpdateStatus(ms)
if updateErr == nil {
return updatedMS, nil
}
// Stop retrying if we exceed statusUpdateRetries - the machineSet will be requeued with a rate limit.
if i >= statusUpdateRetries {
break
}
// Update the MachineSet with the latest resource version for the next poll
if ms, getErr = c.Get(ms.Name, metav1.GetOptions{}); getErr != nil {
// If the GET fails we can't trust status.Replicas anymore. This error
// is bound to be more interesting than the update failure.
return nil, getErr
}
}

return nil, updateErr
}

func isNodeAvailable(node *corev1.Node, minReadySeconds int32, now metav1.Time) bool {
if !isNodeReady(node) {
return false
}

if minReadySeconds == 0 {
return true
}

minReadySecondsDuration := time.Duration(minReadySeconds) * time.Second
_, readyCondition := getNodeCondition(&node.Status, corev1.NodeReady)

if !readyCondition.LastTransitionTime.IsZero() &&
readyCondition.LastTransitionTime.Add(minReadySecondsDuration).Before(now.Time) {
return true
}

return false
}

// getNodeCondition extracts the provided condition from the given status and returns that.
// Returns nil and -1 if the condition is not present, and the index of the located condition.
func getNodeCondition(status *corev1.NodeStatus, conditionType corev1.NodeConditionType) (int, *corev1.NodeCondition) {
if status == nil {
return -1, nil
}
for i := range status.Conditions {
if status.Conditions[i].Type == conditionType {
return i, &status.Conditions[i]
}
}
return -1, nil
}

// isNodeReady returns true if a node is ready; false otherwise.
func isNodeReady(node *corev1.Node) bool {
for _, c := range node.Status.Conditions {
if c.Type == corev1.NodeReady {
return c.Status == corev1.ConditionTrue
}
}
return false
}

func getMachinesToDelete(filteredMachines []*v1alpha1.Machine, diff int) []*v1alpha1.Machine {
// TODO: Define machines deletion policies.
// see: https://github.com/kubernetes/kube-deploy/issues/625
return filteredMachines[:diff]
}
Loading

0 comments on commit b2ad52a

Please sign in to comment.