/
identified_resources.go
104 lines (79 loc) · 2.83 KB
/
identified_resources.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2024 The Carvel Authors.
// SPDX-License-Identifier: Apache-2.0
package resources
import (
"fmt"
"github.com/vmware-tanzu/carvel-kapp/pkg/kapp/logger"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
)
type IdentifiedResources struct {
coreClient kubernetes.Interface
resourceTypes ResourceTypes
resources Resources
fallbackAllowedNamespaces []string
logger logger.Logger
}
func NewIdentifiedResources(coreClient kubernetes.Interface, resourceTypes ResourceTypes,
resources Resources, fallbackAllowedNamespaces []string, logger logger.Logger) IdentifiedResources {
return IdentifiedResources{coreClient, resourceTypes, resources,
fallbackAllowedNamespaces, logger.NewPrefixed("IdentifiedResources")}
}
func (r IdentifiedResources) Create(resource Resource) (Resource, error) {
defer r.logger.DebugFunc(fmt.Sprintf("Create(%s)", resource.Description())).Finish()
resource = resource.DeepCopy()
err := NewIdentityAnnotation(resource).AddMod().Apply(resource)
if err != nil {
return nil, err
}
resource, err = r.resources.Create(resource)
if err != nil {
return nil, err
}
err = NewIdentityAnnotation(resource).RemoveMod().Apply(resource)
if err != nil {
return nil, err
}
return resource, nil
}
func (r IdentifiedResources) Update(resource Resource) (Resource, error) {
defer r.logger.DebugFunc(fmt.Sprintf("Update(%s)", resource.Description())).Finish()
resource = resource.DeepCopy()
err := NewIdentityAnnotation(resource).AddMod().Apply(resource)
if err != nil {
return nil, err
}
resource, err = r.resources.Update(resource)
if err != nil {
return nil, err
}
err = NewIdentityAnnotation(resource).RemoveMod().Apply(resource)
if err != nil {
return nil, err
}
return resource, nil
}
func (r IdentifiedResources) Patch(resource Resource, patchType types.PatchType, data []byte) (Resource, error) {
defer r.logger.DebugFunc(fmt.Sprintf("Patch(%s)", resource.Description())).Finish()
return r.resources.Patch(resource, patchType, data)
}
func (r IdentifiedResources) Delete(resource Resource) error {
defer r.logger.DebugFunc(fmt.Sprintf("Delete(%s)", resource.Description())).Finish()
return r.resources.Delete(resource)
}
func (r IdentifiedResources) Get(resource Resource) (Resource, error) {
defer r.logger.DebugFunc(fmt.Sprintf("Get(%s)", resource.Description())).Finish()
resource, err := r.resources.Get(resource)
if err != nil {
return nil, err
}
err = NewIdentityAnnotation(resource).RemoveMod().Apply(resource)
if err != nil {
return nil, err
}
return resource, nil
}
func (r IdentifiedResources) Exists(resource Resource, existsOpts ExistsOpts) (Resource, bool, error) {
defer r.logger.DebugFunc(fmt.Sprintf("Exists(%s)", resource.Description())).Finish()
return r.resources.Exists(resource, existsOpts)
}