Commit 7389b341 authored by Adam Reese's avatar Adam Reese Committed by GitHub
Browse files

Merge pull request #1836 from adamreese/ref/kube-cleanup

ref(kube): misc code cleanup
parents 9dae8eca de64da2b
main Release add-codeql dependabot/go_modules/github.com/docker/distribution-2.8.2incompatible dependabot/go_modules/github.com/lib/pq-1.10.9 dependabot/go_modules/github.com/rubenv/sql-migrate-1.4.0 dependabot/go_modules/golang.org/x/crypto-0.9.0 dependabot/go_modules/golang.org/x/term-0.8.0 dependabot/go_modules/k8s.io/klog/v2-2.100.1 dev-v2 feat-v3/event-emitter-lua release-2.10 release-2.11 release-2.12 release-2.13 release-2.14 release-2.15 release-2.16 release-2.17 release-2.2 release-2.3 release-2.4 release-2.5 release-2.6 release-2.7 release-2.8 release-2.9 release-3.0 release-3.1 release-3.10 release-3.11 release-3.12 release-3.2 release-3.3 release-3.4 release-3.5 release-3.6 release-3.6.1 release-3.6.2 release-3.7 release-3.8 release-3.9 release-v3.0.0-beta.4 v3.12.0 v3.12.0-rc.1 v3.12.0-dev.1 v3.11.3 v3.11.2 v3.11.1 v3.11.0 v3.11.0-rc.2 v3.11.0-rc.1 v3.10.3 v3.10.2 v3.10.1 v3.10.0 v3.10.0-rc.1 v3.9.4 v3.9.3 v3.9.2 v3.9.1 v3.9.0 v3.9.0-rc.1 v3.8.2 v3.8.1 v3.8.0 v3.8.0-rc.2 v3.8.0-rc.1 v3.7.2 v3.7.1 v3.7.0 v3.7.0-rc.3 v3.7.0-rc.2 v3.7.0-rc.1 v3.6.3 v3.6.2 v3.6.1 v3.6.0 v3.6.0-rc.1 v3.5.4 v3.5.3 v3.5.2 v3.5.1 v3.5.0 v3.5.0-rc.2 v3.5.0-rc.1 v3.4.2 v3.4.1 v3.4.0 v3.4.0-rc.1 v3.3.4 v3.3.3 v3.3.2 v3.3.1 v3.3.0 v3.3.0-rc.2 v3.3.0-rc.1 v3.2.4 v3.2.3 v3.2.2 v3.2.1 v3.2.0 v3.2.0-rc.1 v3.1.3 v3.1.2 v3.1.1 v3.1.0 v3.1.0-rc.3 v3.1.0-rc.2 v3.1.0-rc.1 v3.0.3 v3.0.2 v3.0.1 v3.0.0 v3.0.0-rc.4 v3.0.0-rc.3 v3.0.0-rc.2 v3.0.0-rc.1 v3.0.0-beta.5 v3.0.0-beta.4 v3.0.0-beta.3 v3.0.0-beta.2 v3.0.0-beta.1 v3.0.0-alpha.2 v3.0.0-alpha.1 v2.17.0 v2.17.0-rc.1 v2.16.12 v2.16.11 v2.16.10 v2.16.9 v2.16.8 v2.16.7 v2.16.6 v2.16.5 v2.16.4 v2.16.3 v2.16.2 v2.16.1 v2.16.0 v2.16.0-rc.2 v2.16.0-rc.1 v2.15.2 v2.15.1 v2.15.0 v2.15.0-rc.2 v2.15.0-rc.1 v2.14.3 v2.14.2 v2.14.1 v2.14.0 v2.14.0-rc.2 v2.14.0-rc.1 v2.13.1 v2.13.1-rc.1 v2.13.0 v2.13.0-rc.2 v2.13.0-rc.1 v2.12.3 v2.12.2 v2.12.1 v2.12.0 v2.12.0-rc.2 v2.12.0-rc.1 v2.11.0 v2.11.0-rc.4 v2.11.0-rc.3 v2.11.0-rc.2 v2.11.0-rc.1 v2.10.0 v2.10.0-rc.3 v2.10.0-rc.2 v2.10.0-rc.1 v2.9.1 v2.9.0 v2.9.0-rc5 v2.9.0-rc4 v2.9.0-rc3 v2.9.0-rc2 v2.9.0-rc1 v2.8.2 v2.8.2-rc1 v2.8.1 v2.8.0 v2.8.0-rc.1 v2.7.2 v2.7.1 v2.7.0 v2.7.0-rc1 v2.6.2 v2.6.1 v2.6.0 v2.5.1 v2.5.0 v2.4.2 v2.4.1 v2.4.0 v2.3.1 v2.3.0 v2.2.3 v2.2.2 v2.2.1 v2.2.0
No related merge requests found
Showing with 34 additions and 39 deletions
+34 -39
...@@ -97,7 +97,7 @@ func (c *Client) newBuilder(namespace string, reader io.Reader) *resource.Result ...@@ -97,7 +97,7 @@ func (c *Client) newBuilder(namespace string, reader io.Reader) *resource.Result
func (c *Client) Build(namespace string, reader io.Reader) (Result, error) { func (c *Client) Build(namespace string, reader io.Reader) (Result, error) {
var result Result var result Result
result, err := c.newBuilder(namespace, reader).Infos() result, err := c.newBuilder(namespace, reader).Infos()
return result, err return result, scrubValidationError(err)
} }
// Get gets kubernetes resources as pretty printed string // Get gets kubernetes resources as pretty printed string
...@@ -126,6 +126,9 @@ func (c *Client) Get(namespace string, reader io.Reader) (string, error) { ...@@ -126,6 +126,9 @@ func (c *Client) Get(namespace string, reader io.Reader) (string, error) {
objs[objType] = append(objs[objType], obj) objs[objType] = append(objs[objType], obj)
return nil return nil
}) })
if err != nil {
return "", err
}
// Ok, now we have all the objects grouped by types (say, by v1/Pod, v1/Service, etc.), so // Ok, now we have all the objects grouped by types (say, by v1/Pod, v1/Service, etc.), so
// spin through them and print them. Printer is cool since it prints the header only when // spin through them and print them. Printer is cool since it prints the header only when
...@@ -134,23 +137,20 @@ func (c *Client) Get(namespace string, reader io.Reader) (string, error) { ...@@ -134,23 +137,20 @@ func (c *Client) Get(namespace string, reader io.Reader) (string, error) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
p := kubectl.NewHumanReadablePrinter(kubectl.PrintOptions{}) p := kubectl.NewHumanReadablePrinter(kubectl.PrintOptions{})
for t, ot := range objs { for t, ot := range objs {
_, err = buf.WriteString("==> " + t + "\n") if _, err = buf.WriteString("==> " + t + "\n"); err != nil {
if err != nil {
return "", err return "", err
} }
for _, o := range ot { for _, o := range ot {
err = p.PrintObj(o, buf) if err := p.PrintObj(o, buf); err != nil {
if err != nil {
log.Printf("failed to print object type '%s', object: '%s' :\n %v", t, o, err) log.Printf("failed to print object type '%s', object: '%s' :\n %v", t, o, err)
return "", err return "", err
} }
} }
_, err := buf.WriteString("\n") if _, err := buf.WriteString("\n"); err != nil {
if err != nil {
return "", err return "", err
} }
} }
return buf.String(), err return buf.String(), nil
} }
// Update reads in the current configuration and a target configuration from io.reader // Update reads in the current configuration and a target configuration from io.reader
...@@ -211,9 +211,10 @@ func (c *Client) Update(namespace string, originalReader, targetReader io.Reader ...@@ -211,9 +211,10 @@ func (c *Client) Update(namespace string, originalReader, targetReader io.Reader
return nil return nil
}) })
if err != nil { switch {
case err != nil:
return err return err
} else if len(updateErrors) != 0 { case len(updateErrors) != 0:
return fmt.Errorf(strings.Join(updateErrors, " && ")) return fmt.Errorf(strings.Join(updateErrors, " && "))
} }
...@@ -238,7 +239,7 @@ func (c *Client) Delete(namespace string, reader io.Reader) error { ...@@ -238,7 +239,7 @@ func (c *Client) Delete(namespace string, reader io.Reader) error {
} }
func skipIfNotFound(err error) error { func skipIfNotFound(err error) error {
if err != nil && errors.IsNotFound(err) { if errors.IsNotFound(err) {
log.Printf("%v", err) log.Printf("%v", err)
return nil return nil
} }
...@@ -273,7 +274,7 @@ func perform(c *Client, namespace string, reader io.Reader, fn ResourceActorFunc ...@@ -273,7 +274,7 @@ func perform(c *Client, namespace string, reader io.Reader, fn ResourceActorFunc
infos, err := c.Build(namespace, reader) infos, err := c.Build(namespace, reader)
switch { switch {
case err != nil: case err != nil:
return scrubValidationError(err) return err
case len(infos) == 0: case len(infos) == 0:
return ErrNoObjectsVisited return ErrNoObjectsVisited
} }
...@@ -327,33 +328,30 @@ func updateResource(c *Client, target *resource.Info, currentObj runtime.Object, ...@@ -327,33 +328,30 @@ func updateResource(c *Client, target *resource.Info, currentObj runtime.Object,
// send patch to server // send patch to server
helper := resource.NewHelper(target.Client, target.Mapping) helper := resource.NewHelper(target.Client, target.Mapping)
_, err = helper.Patch(target.Namespace, target.Name, api.StrategicMergePatchType, patch) if _, err = helper.Patch(target.Namespace, target.Name, api.StrategicMergePatchType, patch); err != nil {
if err != nil {
return err return err
} }
if recreate { if recreate {
kind := target.Mapping.GroupVersionKind.Kind
client, _ := c.ClientSet() client, _ := c.ClientSet()
switch kind { return recreatePods(client, target.Namespace, extractSelector(currentObj))
case "ReplicationController":
rc := currentObj.(*v1.ReplicationController)
err = recreatePods(client, target.Namespace, rc.Spec.Selector)
case "DaemonSet":
daemonSet := currentObj.(*v1beta1.DaemonSet)
err = recreatePods(client, target.Namespace, daemonSet.Spec.Selector.MatchLabels)
case "StatefulSet":
petSet := currentObj.(*apps.StatefulSet)
err = recreatePods(client, target.Namespace, petSet.Spec.Selector.MatchLabels)
case "ReplicaSet":
replicaSet := currentObj.(*v1beta1.ReplicaSet)
err = recreatePods(client, target.Namespace, replicaSet.Spec.Selector.MatchLabels)
}
} }
return nil
}
return err func extractSelector(obj runtime.Object) map[string]string {
switch typed := obj.(type) {
case *v1.ReplicationController:
return typed.Spec.Selector
case *v1beta1.DaemonSet:
return typed.Spec.Selector.MatchLabels
case *apps.StatefulSet:
return typed.Spec.Selector.MatchLabels
case *v1beta1.ReplicaSet:
return typed.Spec.Selector.MatchLabels
default:
return nil
}
} }
func recreatePods(client *internalclientset.Clientset, namespace string, selector map[string]string) error { func recreatePods(client *internalclientset.Clientset, namespace string, selector map[string]string) error {
...@@ -371,13 +369,7 @@ func recreatePods(client *internalclientset.Clientset, namespace string, selecto ...@@ -371,13 +369,7 @@ func recreatePods(client *internalclientset.Clientset, namespace string, selecto
log.Printf("Restarting pod: %v/%v", pod.Namespace, pod.Name) log.Printf("Restarting pod: %v/%v", pod.Namespace, pod.Name)
// Delete each pod for get them restarted with changed spec. // Delete each pod for get them restarted with changed spec.
err := client.Pods(pod.Namespace).Delete(pod.Name, &api.DeleteOptions{ if err := client.Pods(pod.Namespace).Delete(pod.Name, api.NewPreconditionDeleteOptions(string(pod.UID))); err != nil {
Preconditions: &api.Preconditions{
UID: &pod.UID,
},
})
if err != nil {
return err return err
} }
} }
...@@ -449,6 +441,9 @@ func waitForJob(e watch.Event, name string) (bool, error) { ...@@ -449,6 +441,9 @@ func waitForJob(e watch.Event, name string) (bool, error) {
// scrubValidationError removes kubectl info from the message // scrubValidationError removes kubectl info from the message
func scrubValidationError(err error) error { func scrubValidationError(err error) error {
if err == nil {
return nil
}
const stopValidateMessage = "if you choose to ignore these errors, turn validation off with --validate=false" const stopValidateMessage = "if you choose to ignore these errors, turn validation off with --validate=false"
if strings.Contains(err.Error(), stopValidateMessage) { if strings.Contains(err.Error(), stopValidateMessage) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment