Skip to content

Commit

Permalink
Add Resources interface
Browse files Browse the repository at this point in the history
- Change Resources type to Impl
  • Loading branch information
gcheadle-vmware committed Mar 4, 2021
1 parent fc35325 commit 2ab6fdd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pkg/kapp/cmd/app/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func FactoryClients(depsFactory cmdcore.DepsFactory, nsFlags cmdcore.NamespaceFl
CanIgnoreFailingAPIService: resTypesFlags.CanIgnoreFailingAPIService,
})

resources := ctlres.NewResources(resTypes, coreClient, dynamicClient, fallbackAllowedNss, logger)
resources := ctlres.NewImpl(resTypes, coreClient, dynamicClient, fallbackAllowedNss, logger)

identifiedResources := ctlres.NewIdentifiedResources(
coreClient, resTypes, resources, fallbackAllowedNss, logger)
Expand Down
2 changes: 1 addition & 1 deletion pkg/kapp/cmd/tools/list_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (o *ListLabelsOptions) listResources() ([]ctlres.Resource, error) {
}

resTypes := ctlres.NewResourceTypesImpl(coreClient, ctlres.ResourceTypesImplOpts{})
resources := ctlres.NewResources(resTypes, coreClient, dynamicClient, nil, o.logger)
resources := ctlres.NewImpl(resTypes, coreClient, dynamicClient, nil, o.logger)
identifiedResources := ctlres.NewIdentifiedResources(coreClient, resTypes, resources, nil, o.logger)

labelSelector, err := labels.Parse("!kapp")
Expand Down
4 changes: 2 additions & 2 deletions pkg/kapp/resources/identified_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
type IdentifiedResources struct {
coreClient kubernetes.Interface
resourceTypes ResourceTypes
resources *Resources
resources Resources
fallbackAllowedNamespaces []string
logger logger.Logger
}

func NewIdentifiedResources(coreClient kubernetes.Interface, resourceTypes ResourceTypes,
resources *Resources, fallbackAllowedNamespaces []string, logger logger.Logger) IdentifiedResources {
resources Resources, fallbackAllowedNamespaces []string, logger logger.Logger) IdentifiedResources {

return IdentifiedResources{coreClient, resourceTypes, resources,
fallbackAllowedNamespaces, logger.NewPrefixed("IdentifiedResources")}
Expand Down
46 changes: 28 additions & 18 deletions pkg/kapp/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@ const (
resourcesDebug = false
)

type Resources struct {
type Resources interface {
All([]ResourceType, AllOpts) ([]Resource, error)
Delete(Resource) error
Exists(Resource) (bool, error)
Get(Resource) (Resource, error)
Patch(Resource, types.PatchType, []byte) (Resource, error)
Update(Resource) (Resource, error)
Create(resource Resource) (Resource, error)
}

type Impl struct {
resourceTypes ResourceTypes
coreClient kubernetes.Interface
dynamicClient dynamic.Interface
Expand All @@ -49,10 +59,10 @@ type Resources struct {
logger logger.Logger
}

func NewResources(resourceTypes ResourceTypes, coreClient kubernetes.Interface,
dynamicClient dynamic.Interface, fallbackAllowedNamespaces []string, logger logger.Logger) *Resources {
func NewImpl(resourceTypes ResourceTypes, coreClient kubernetes.Interface,
dynamicClient dynamic.Interface, fallbackAllowedNamespaces []string, logger logger.Logger) *Impl {

return &Resources{
return &Impl{
resourceTypes: resourceTypes,
coreClient: coreClient,
dynamicClient: dynamicClient,
Expand All @@ -66,7 +76,7 @@ type unstructItems struct {
Items []unstructured.Unstructured
}

func (c *Resources) All(resTypes []ResourceType, opts AllOpts) ([]Resource, error) {
func (c *Impl) All(resTypes []ResourceType, opts AllOpts) ([]Resource, error) {
defer c.logger.DebugFunc("All").Finish()

if opts.ListOpts == nil {
Expand Down Expand Up @@ -154,7 +164,7 @@ func (c *Resources) All(resTypes []ResourceType, opts AllOpts) ([]Resource, erro
return resources, nil
}

func (c *Resources) allForNamespaces(client dynamic.NamespaceableResourceInterface, listOpts *metav1.ListOptions) (*unstructured.UnstructuredList, error) {
func (c *Impl) allForNamespaces(client dynamic.NamespaceableResourceInterface, listOpts *metav1.ListOptions) (*unstructured.UnstructuredList, error) {
defer c.logger.DebugFunc("allForNamespaces").Finish()

allowedNs, err := c.assumedAllowedNamespaces()
Expand Down Expand Up @@ -202,7 +212,7 @@ func (c *Resources) allForNamespaces(client dynamic.NamespaceableResourceInterfa
return list, nil
}

func (c *Resources) Create(resource Resource) (Resource, error) {
func (c *Impl) Create(resource Resource) (Resource, error) {
if resourcesDebug {
t1 := time.Now().UTC()
defer func() { c.logger.Debug("create %s", time.Now().UTC().Sub(t1)) }()
Expand All @@ -229,7 +239,7 @@ func (c *Resources) Create(resource Resource) (Resource, error) {
return NewResourceUnstructured(*createdUn, resType), nil
}

func (c *Resources) Update(resource Resource) (Resource, error) {
func (c *Impl) Update(resource Resource) (Resource, error) {
if resourcesDebug {
t1 := time.Now().UTC()
defer func() { c.logger.Debug("update %s", time.Now().UTC().Sub(t1)) }()
Expand All @@ -256,7 +266,7 @@ func (c *Resources) Update(resource Resource) (Resource, error) {
return NewResourceUnstructured(*updatedUn, resType), nil
}

func (c *Resources) Patch(resource Resource, patchType types.PatchType, data []byte) (Resource, error) {
func (c *Impl) Patch(resource Resource, patchType types.PatchType, data []byte) (Resource, error) {
if resourcesDebug {
t1 := time.Now().UTC()
defer func() { c.logger.Debug("patch %s", time.Now().UTC().Sub(t1)) }()
Expand All @@ -280,7 +290,7 @@ func (c *Resources) Patch(resource Resource, patchType types.PatchType, data []b
return NewResourceUnstructured(*patchedUn, resType), nil
}

func (c *Resources) Delete(resource Resource) error {
func (c *Impl) Delete(resource Resource) error {
if resourcesDebug {
t1 := time.Now().UTC()
defer func() { c.logger.Debug("delete %s", time.Now().UTC().Sub(t1)) }()
Expand Down Expand Up @@ -326,7 +336,7 @@ func (c *Resources) Delete(resource Resource) error {
return nil
}

func (c *Resources) Get(resource Resource) (Resource, error) {
func (c *Impl) Get(resource Resource) (Resource, error) {
if resourcesDebug {
t1 := time.Now().UTC()
defer func() { c.logger.Debug("get %s", time.Now().UTC().Sub(t1)) }()
Expand All @@ -351,7 +361,7 @@ func (c *Resources) Get(resource Resource) (Resource, error) {
return NewResourceUnstructured(*item, resType), nil
}

func (c *Resources) Exists(resource Resource) (bool, error) {
func (c *Impl) Exists(resource Resource) (bool, error) {
if resourcesDebug {
t1 := time.Now().UTC()
defer func() { c.logger.Debug("exists %s", time.Now().UTC().Sub(t1)) }()
Expand Down Expand Up @@ -406,7 +416,7 @@ var (
podMetricsNotFoundErrCheck = regexp.MustCompile("Error while getting pod (.+) not found \\(reason: \\)")
)

func (c *Resources) isPodMetrics(resource Resource, err error) bool {
func (c *Impl) isPodMetrics(resource Resource, err error) bool {
// Abnormal error case. Get/Delete on PodMetrics may fail
// without NotFound reason due to its dependence on Pod existance
if resource.Kind() == "PodMetrics" && resource.APIGroup() == "metrics.k8s.io" {
Expand All @@ -417,11 +427,11 @@ func (c *Resources) isPodMetrics(resource Resource, err error) bool {
return false
}

func (c *Resources) isGeneralRetryableErr(err error) bool {
func (c *Impl) isGeneralRetryableErr(err error) bool {
return IsResourceChangeBlockedErr(err) || c.isServerRescaleErr(err)
}

func (*Resources) isServerRescaleErr(err error) bool {
func (*Impl) isServerRescaleErr(err error) bool {
switch err := err.(type) {
case *http2.GoAwayError:
return true
Expand All @@ -433,14 +443,14 @@ func (*Resources) isServerRescaleErr(err error) bool {
return false
}

func (c *Resources) resourceErr(err error, action string, resource Resource) error {
func (c *Impl) resourceErr(err error, action string, resource Resource) error {
if typedErr, ok := err.(errors.APIStatus); ok {
return resourceStatusErr{resourcePlainErr{err, action, resource}, typedErr.Status()}
}
return resourcePlainErr{err, action, resource}
}

func (c *Resources) resourceClient(resource Resource) (dynamic.ResourceInterface, ResourceType, error) {
func (c *Impl) resourceClient(resource Resource) (dynamic.ResourceInterface, ResourceType, error) {
resType, err := c.resourceTypes.Find(resource)
if err != nil {
return nil, ResourceType{}, err
Expand All @@ -449,7 +459,7 @@ func (c *Resources) resourceClient(resource Resource) (dynamic.ResourceInterface
return c.dynamicClient.Resource(resType.GroupVersionResource).Namespace(resource.Namespace()), resType, nil
}

func (c *Resources) assumedAllowedNamespaces() ([]string, error) {
func (c *Impl) assumedAllowedNamespaces() ([]string, error) {
c.assumedAllowedNamespacesMemoLock.Lock()
defer c.assumedAllowedNamespacesMemoLock.Unlock()

Expand Down

0 comments on commit 2ab6fdd

Please sign in to comment.