-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
168 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package dao | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/derailed/k9s/internal/client" | ||
"github.com/rs/zerolog/log" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/dynamic" | ||
"k8s.io/client-go/restmapper" | ||
"k8s.io/client-go/scale" | ||
) | ||
|
||
var _ Scalable = (*Scaler)(nil) | ||
var _ ReplicasGetter = (*Scaler)(nil) | ||
|
||
// Scaler represents a generic resource with scaling. | ||
type Scaler struct { | ||
Generic | ||
} | ||
|
||
// Replicas returns the number of replicas for the resource located at the given path. | ||
func (s *Scaler) Replicas(ctx context.Context, path string) (int32, error) { | ||
scaleClient, err := s.scaleClient() | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
ns, name := client.Namespaced(path) | ||
currScale, err := scaleClient.Scales(ns).Get(ctx, *s.gvr.GR(), name, metav1.GetOptions{}) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return currScale.Spec.Replicas, nil | ||
} | ||
|
||
// Scale modifies the number of replicas for a given resource specified by the path. | ||
func (s *Scaler) Scale(ctx context.Context, path string, replicas int32) error { | ||
ns, name := client.Namespaced(path) | ||
|
||
scaleClient, err := s.scaleClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
currentScale, err := scaleClient.Scales(ns).Get(ctx, *s.gvr.GR(), name, metav1.GetOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
currentScale.Spec.Replicas = replicas | ||
updatedScale, err := scaleClient.Scales(ns).Update(ctx, *s.gvr.GR(), currentScale, metav1.UpdateOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Debug().Msgf("%s scaled to %d", path, updatedScale.Spec.Replicas) | ||
return nil | ||
} | ||
|
||
func (s *Scaler) scaleClient() (scale.ScalesGetter, error) { | ||
cfg, err := s.Client().RestConfig() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
discoveryClient, err := s.Client().CachedDiscovery() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
mapper := restmapper.NewDeferredDiscoveryRESTMapper(discoveryClient) | ||
scaleKindResolver := scale.NewDiscoveryScaleKindResolver(discoveryClient) | ||
|
||
return scale.NewForConfig(cfg, mapper, dynamic.LegacyAPIPathResolverFunc, scaleKindResolver) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters