diff --git a/pkg/kapp/cmd/app/factory.go b/pkg/kapp/cmd/app/factory.go index 30400b2b3..ee03985fc 100644 --- a/pkg/kapp/cmd/app/factory.go +++ b/pkg/kapp/cmd/app/factory.go @@ -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) diff --git a/pkg/kapp/cmd/tools/list_labels.go b/pkg/kapp/cmd/tools/list_labels.go index c808a3856..2f599b52f 100644 --- a/pkg/kapp/cmd/tools/list_labels.go +++ b/pkg/kapp/cmd/tools/list_labels.go @@ -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") diff --git a/pkg/kapp/resources/identified_resources.go b/pkg/kapp/resources/identified_resources.go index cd58cb972..76caacad6 100644 --- a/pkg/kapp/resources/identified_resources.go +++ b/pkg/kapp/resources/identified_resources.go @@ -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")} diff --git a/pkg/kapp/resources/resources.go b/pkg/kapp/resources/resources.go index e10e6d474..77928e1c4 100644 --- a/pkg/kapp/resources/resources.go +++ b/pkg/kapp/resources/resources.go @@ -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 @@ -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, @@ -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 { @@ -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() @@ -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)) }() @@ -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)) }() @@ -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)) }() @@ -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)) }() @@ -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)) }() @@ -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)) }() @@ -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" { @@ -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 @@ -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 @@ -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()