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

Pass the cloud object to validator from caller #7925

Merged
merged 2 commits into from
Dec 3, 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: 1 addition & 1 deletion cmd/kops/rollingupdatecluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func RunRollingUpdateCluster(f *util.Factory, out io.Writer, options *RollingUpd
if featureflag.DrainAndValidateRollingUpdate.Enabled() {
klog.V(2).Infof("Rolling update with drain and validate enabled.")
if !options.CloudOnly {
clusterValidator, err = validation.NewClusterValidator(cluster, list, k8sClient)
clusterValidator, err = validation.NewClusterValidator(cluster, cloud, list, k8sClient)
if err != nil {
return fmt.Errorf("cannot create cluster validator: %v", err)
}
Expand Down
9 changes: 8 additions & 1 deletion cmd/kops/validate_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"strings"
"time"

"k8s.io/kops/upup/pkg/fi/cloudup"

"github.com/ghodss/yaml"
"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -93,6 +95,11 @@ func RunValidateCluster(f *util.Factory, cmd *cobra.Command, args []string, out
return nil, err
}

cloud, err := cloudup.BuildCloud(cluster)
if err != nil {
return nil, err
}

clientSet, err := f.Clientset()
if err != nil {
return nil, err
Expand Down Expand Up @@ -134,7 +141,7 @@ func RunValidateCluster(f *util.Factory, cmd *cobra.Command, args []string, out
timeout := time.Now().Add(options.wait)
pollInterval := 10 * time.Second

validator, err := validation.NewClusterValidator(cluster, list, k8sClient)
validator, err := validation.NewClusterValidator(cluster, cloud, list, k8sClient)
if err != nil {
return nil, fmt.Errorf("unexpected error creating validatior: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/validation/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ go_library(
"//pkg/apis/kops/util:go_default_library",
"//pkg/cloudinstances:go_default_library",
"//pkg/dns:go_default_library",
"//upup/pkg/fi/cloudup:go_default_library",
"//upup/pkg/fi:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/client-go/kubernetes:go_default_library",
Expand Down
71 changes: 32 additions & 39 deletions pkg/validation/validate_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"net/url"
"strings"

"k8s.io/kops/upup/pkg/fi"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
Expand All @@ -31,7 +33,6 @@ import (
"k8s.io/kops/pkg/apis/kops/util"
"k8s.io/kops/pkg/cloudinstances"
"k8s.io/kops/pkg/dns"
"k8s.io/kops/upup/pkg/fi/cloudup"
)

// ValidationCluster uses a cluster to validate.
Expand All @@ -54,9 +55,10 @@ type ClusterValidator interface {
}

type clusterValidatorImpl struct {
cluster *kops.Cluster
instanceGroupList *kops.InstanceGroupList
k8sClient kubernetes.Interface
cluster *kops.Cluster
cloud fi.Cloud
instanceGroups []*kops.InstanceGroup
k8sClient kubernetes.Interface
}

func (v *ValidationCluster) addError(failure *ValidationError) {
Expand Down Expand Up @@ -100,23 +102,30 @@ func hasPlaceHolderIP(clusterName string) (bool, error) {
return false, nil
}

func NewClusterValidator(cluster *kops.Cluster, instanceGroupList *kops.InstanceGroupList, k8sClient kubernetes.Interface) (ClusterValidator, error) {
func NewClusterValidator(cluster *kops.Cluster, cloud fi.Cloud, instanceGroupList *kops.InstanceGroupList, k8sClient kubernetes.Interface) (ClusterValidator, error) {
var instanceGroups []*kops.InstanceGroup

for i := range instanceGroupList.Items {
ig := &instanceGroupList.Items[i]
instanceGroups = append(instanceGroups, ig)
}

if len(instanceGroups) == 0 {
return nil, fmt.Errorf("no InstanceGroup objects found")
}

return &clusterValidatorImpl{
cluster: cluster,
instanceGroupList: instanceGroupList,
k8sClient: k8sClient,
cluster: cluster,
cloud: cloud,
instanceGroups: instanceGroups,
k8sClient: k8sClient,
}, nil
}

func (v *clusterValidatorImpl) Validate() (*ValidationCluster, error) {
return validateCluster(v.cluster, v.instanceGroupList, v.k8sClient)
}

// validateCluster validates a k8s cluster with a provided instance group list
func validateCluster(cluster *kops.Cluster, instanceGroupList *kops.InstanceGroupList, k8sClient kubernetes.Interface) (*ValidationCluster, error) {
clusterName := cluster.Name
clusterName := v.cluster.Name

v := &ValidationCluster{}
validation := &ValidationCluster{}

// Do not use if we are running gossip
if !dns.IsGossipHostname(clusterName) {
Expand All @@ -134,52 +143,36 @@ func validateCluster(cluster *kops.Cluster, instanceGroupList *kops.InstanceGrou
" Please wait about 5-10 minutes for a master to start, dns-controller to launch, and DNS to propagate." +
" The protokube container and dns-controller deployment logs may contain more diagnostic information." +
" Etcd and the API DNS entries must be updated for a kops Kubernetes cluster to start."
v.addError(&ValidationError{
validation.addError(&ValidationError{
Kind: "dns",
Name: "apiserver",
Message: message,
})
return v, nil
return validation, nil
}
}

var instanceGroups []*kops.InstanceGroup

for i := range instanceGroupList.Items {
ig := &instanceGroupList.Items[i]
instanceGroups = append(instanceGroups, ig)
}

if len(instanceGroups) == 0 {
return nil, fmt.Errorf("no InstanceGroup objects found")
}

cloud, err := cloudup.BuildCloud(cluster)
if err != nil {
return nil, err
}

nodeList, err := k8sClient.CoreV1().Nodes().List(metav1.ListOptions{})
nodeList, err := v.k8sClient.CoreV1().Nodes().List(metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("error listing nodes: %v", err)
}

warnUnmatched := false
cloudGroups, err := cloud.GetCloudGroups(cluster, instanceGroups, warnUnmatched, nodeList.Items)
cloudGroups, err := v.cloud.GetCloudGroups(v.cluster, v.instanceGroups, warnUnmatched, nodeList.Items)
if err != nil {
return nil, err
}
v.validateNodes(cloudGroups)
validation.validateNodes(cloudGroups)

if err := v.collectComponentFailures(k8sClient); err != nil {
if err := validation.collectComponentFailures(v.k8sClient); err != nil {
return nil, fmt.Errorf("cannot get component status for %q: %v", clusterName, err)
}

if err = v.collectPodFailures(k8sClient); err != nil {
if err = validation.collectPodFailures(v.k8sClient); err != nil {
return nil, fmt.Errorf("cannot get pod health for %q: %v", clusterName, err)
}

return v, nil
return validation, nil
}

func (v *ValidationCluster) collectComponentFailures(client kubernetes.Interface) error {
Expand Down