-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource.go
81 lines (65 loc) · 2.21 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
78
79
80
81
package appcatalog
import (
"context"
"fmt"
"github.com/giantswarm/apiextensions/pkg/apis/application/v1alpha1"
"github.com/giantswarm/apiextensions/pkg/clientset/versioned"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/giantswarm/app-operator/service/controller/app/controllercontext"
"github.com/giantswarm/app-operator/service/controller/app/key"
)
const (
// Name is the identifier of the resource.
Name = "appcatalog"
)
// Config represents the configuration used to create a new appcatalog resource.
type Config struct {
// Dependencies.
G8sClient versioned.Interface
Logger micrologger.Logger
}
// Resource implements the appcatalog resource.
type Resource struct {
// Dependencies.
g8sClient versioned.Interface
logger micrologger.Logger
}
// New creates a new configured appcatalog resource.
func New(config Config) (*Resource, error) {
if config.G8sClient == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.G8sClient must not be empty", config)
}
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Logger must not be empty", config)
}
r := &Resource{
// Dependencies.
g8sClient: config.G8sClient,
logger: config.Logger,
}
return r, nil
}
func (*Resource) Name() string {
return Name
}
// getCatalogForApp gets the appCatalog CR specified in the provided app CR.
func (r *Resource) getCatalogForApp(ctx context.Context, customResource v1alpha1.App) error {
cc, err := controllercontext.FromContext(ctx)
if err != nil {
return microerror.Mask(err)
}
catalogName := key.CatalogName(customResource)
r.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("looking for appCatalog %#q", catalogName))
appCatalog, err := r.g8sClient.ApplicationV1alpha1().AppCatalogs().Get(catalogName, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
return microerror.Maskf(notFoundError, "appCatalog %#q", catalogName)
} else if err != nil {
return microerror.Mask(err)
}
r.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("found appCatalog %#q", catalogName))
cc.AppCatalog = *appCatalog
return nil
}