Scale scales a resource in a cluster.
(ctx context.Context, clientset client.Clientset, conf Config)
| 54 | |
| 55 | // Scale scales a resource in a cluster. |
| 56 | func Scale(ctx context.Context, clientset client.Clientset, conf Config) error { |
| 57 | if conf.SerialLength == 0 && conf.Replicas > 1 { |
| 58 | return fmt.Errorf("serial length must be greater than 0 when replicas is greater than 1") |
| 59 | } |
| 60 | |
| 61 | param := conf.Parameters |
| 62 | |
| 63 | name := conf.Name |
| 64 | namespace := conf.Namespace |
| 65 | index := 0 |
| 66 | renderer := gotpl.NewRenderer(gotpl.FuncMap{ |
| 67 | "Name": func() string { |
| 68 | return name |
| 69 | }, |
| 70 | "Namespace": func() string { |
| 71 | return namespace |
| 72 | }, |
| 73 | "Index": func() int { |
| 74 | return index |
| 75 | }, |
| 76 | "AddCIDR": utilsnet.AddCIDRStr, |
| 77 | "AddIP": utilsnet.AddIPStr, |
| 78 | }) |
| 79 | data, err := renderer.ToJSON(conf.Template, param) |
| 80 | if err != nil { |
| 81 | return err |
| 82 | } |
| 83 | |
| 84 | if conf.DryRun { |
| 85 | dryrun.PrintMessagef("# Scale resource %s to %d replicas", conf.Name, conf.Replicas) |
| 86 | dryrun.PrintMessagef("# Resource example: %s", string(data)) |
| 87 | return nil |
| 88 | } |
| 89 | |
| 90 | var u *unstructured.Unstructured |
| 91 | err = json.Unmarshal(data, &u) |
| 92 | if err != nil { |
| 93 | return err |
| 94 | } |
| 95 | gv, err := schema.ParseGroupVersion(u.GetAPIVersion()) |
| 96 | if err != nil { |
| 97 | return err |
| 98 | } |
| 99 | |
| 100 | dynamicClient, err := clientset.ToDynamicClient() |
| 101 | if err != nil { |
| 102 | return err |
| 103 | } |
| 104 | |
| 105 | restMapper, err := clientset.ToRESTMapper() |
| 106 | if err != nil { |
| 107 | return err |
| 108 | } |
| 109 | gvr, err := restMapper.ResourceFor(schema.GroupVersionResource{ |
| 110 | Group: gv.Group, |
| 111 | Version: gv.Version, |
| 112 | Resource: u.GetKind(), |
| 113 | }) |
no test coverage detected