-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource.go
77 lines (62 loc) · 1.83 KB
/
resource.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
package appnamespace
import (
"context"
"github.com/giantswarm/apiextensions/pkg/apis/application/v1alpha1"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"github.com/giantswarm/app-operator/service/controller/app/controllercontext"
)
const (
// Name is the identifier of the resource.
Name = "appnamespace"
)
// Config represents the configuration used to create a new appnamespace resource.
type Config struct {
// Dependencies.
K8sClient kubernetes.Interface
Logger micrologger.Logger
}
// Resource implements the appnamespace resource.
type Resource struct {
// Dependencies.
k8sClient kubernetes.Interface
logger micrologger.Logger
}
// New creates a new configured appnamespace resource.
func New(config Config) (*Resource, error) {
if config.K8sClient == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.K8sClient must not be empty", config)
}
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Logger must not be empty", config)
}
r := &Resource{
// Dependencies.
k8sClient: config.K8sClient,
logger: config.Logger,
}
return r, nil
}
func (*Resource) Name() string {
return Name
}
// addNamespaceStatusToContext checks whether the namespace app CR belongs to
// is being deleting currently.
func (r *Resource) addNamespaceStatusToContext(ctx context.Context, cr v1alpha1.App) error {
cc, err := controllercontext.FromContext(ctx)
if err != nil {
return microerror.Mask(err)
}
ns, err := r.k8sClient.CoreV1().Namespaces().Get(cr.Namespace, metav1.GetOptions{})
if err != nil {
return microerror.Mask(err)
}
if ns.GetDeletionTimestamp() != nil {
cc.Status.ClusterStatus.IsDeleting = true
} else {
cc.Status.ClusterStatus.IsDeleting = false
}
return nil
}