Skip to content

Commit

Permalink
fix(platform): patch annotation according to k8s version (#969)
Browse files Browse the repository at this point in the history
  • Loading branch information
huxiaoliang committed Dec 2, 2020
1 parent 311bf67 commit 7ac0421
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
22 changes: 17 additions & 5 deletions pkg/platform/provider/baremetal/cluster/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -1083,11 +1083,23 @@ func (p *Provider) EnsurePatchAnnotation(ctx context.Context, c *v1.Cluster) err
machines := map[bool][]platformv1.ClusterMachine{
true: c.Spec.ScalingMachines,
false: c.Spec.Machines}[len(c.Spec.ScalingMachines) > 0]
client, err := c.Clientset()
if err != nil {
return err
}
// from k8s 1.18, kubeadm will add built-in annotations to etcd and kube-apiserver
// we should handle such case when add tkestack annotations according to different case
prefix := ""
lineIndex := "5"
if apiclient.ClusterVersionIsBefore118(client) {
prefix = ` annotations:\n`
lineIndex = "3"
}
fileData := map[string]string{
constants.EtcdPodManifestFile: ` annotations:\n scheduler.alpha.kubernetes.io/critical-pod: ""\n tke.prometheus.io/scrape: "true"\n prometheus.io/scheme: "https"\n prometheus.io/port: "2379"`,
constants.KubeAPIServerPodManifestFile: ` annotations:\n scheduler.alpha.kubernetes.io/critical-pod: ""\n tke.prometheus.io/scrape: "true"\n prometheus.io/scheme: "https"\n prometheus.io/port: "6443"`,
constants.KubeControllerManagerPodManifestFile: ` annotations:\n scheduler.alpha.kubernetes.io/critical-pod: ""\n tke.prometheus.io/scrape: "true"\n prometheus.io/scheme: "http"\n prometheus.io/port: "10252"`,
constants.KubeSchedulerPodManifestFile: ` annotations:\n scheduler.alpha.kubernetes.io/critical-pod: ""\n tke.prometheus.io/scrape: "true"\n prometheus.io/scheme: "http"\n prometheus.io/port: "10251"`,
constants.EtcdPodManifestFile: prefix + ` scheduler.alpha.kubernetes.io/critical-pod: ""\n tke.prometheus.io/scrape: "true"\n prometheus.io/scheme: "https"\n prometheus.io/port: "2379"`,
constants.KubeAPIServerPodManifestFile: prefix + ` scheduler.alpha.kubernetes.io/critical-pod: ""\n tke.prometheus.io/scrape: "true"\n prometheus.io/scheme: "https"\n prometheus.io/port: "6443"`,
constants.KubeControllerManagerPodManifestFile: prefix + ` scheduler.alpha.kubernetes.io/critical-pod: ""\n tke.prometheus.io/scrape: "true"\n prometheus.io/scheme: "http"\n prometheus.io/port: "10252"`,
constants.KubeSchedulerPodManifestFile: prefix + ` scheduler.alpha.kubernetes.io/critical-pod: ""\n tke.prometheus.io/scrape: "true"\n prometheus.io/scheme: "http"\n prometheus.io/port: "10251"`,
}
for _, machine := range machines {
machineSSH, err := machine.SSH()
Expand All @@ -1096,7 +1108,7 @@ func (p *Provider) EnsurePatchAnnotation(ctx context.Context, c *v1.Cluster) err
}

for file, data := range fileData {
cmd := fmt.Sprintf(`grep 'prometheus.io/port' %s || sed -i '3a\%s' %s`, file, data, file)
cmd := fmt.Sprintf(`grep 'prometheus.io/port' %s || sed -i '%sa\%s' %s`, file, lineIndex, data, file)
_, stderr, exit, err := machineSSH.Exec(cmd)
if err != nil || exit != 0 {
return fmt.Errorf("exec %q failed:exit %d:stderr %s:error %s", cmd, exit, stderr, err)
Expand Down
1 change: 0 additions & 1 deletion pkg/platform/provider/baremetal/cluster/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func (p *Provider) EnsureRemoveNode(ctx context.Context, c *v1.Cluster) error {
if !errors.IsNotFound(err) {
return err
}
log.FromContext(ctx).Info("deleteNode done")
return nil
}
err = client.CoreV1().Nodes().Delete(context.Background(), node.Name, metav1.DeleteOptions{})
Expand Down
9 changes: 9 additions & 0 deletions pkg/util/apiclient/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ func ClusterVersionIsBefore116(client kubernetes.Interface) bool {
return result
}

func ClusterVersionIsBefore118(client kubernetes.Interface) bool {
result, err := CheckClusterVersion(client, "< 1.18")
if err != nil {
return false
}

return result
}

func CheckClusterVersion(client kubernetes.Interface, versionConstraint string) (bool, error) {
version, err := GetClusterVersion(client)
if err != nil {
Expand Down

0 comments on commit 7ac0421

Please sign in to comment.