-
Notifications
You must be signed in to change notification settings - Fork 6
/
service.go
67 lines (51 loc) · 1.38 KB
/
service.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
package clientcert
import (
"context"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"github.com/giantswarm/microerror"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var _ Interface = (*Service)(nil)
type Config struct {
Client client.Client
}
type Service struct {
client client.Client
}
func New(config Config) (*Service, error) {
if config.Client == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Client must not be empty", config)
}
s := &Service{
client: config.Client,
}
return s, nil
}
func (s *Service) Create(ctx context.Context, clientCert *ClientCert) error {
kp := clientCert.Object()
err := s.client.Create(ctx, kp)
if err != nil {
return microerror.Mask(err)
}
return nil
}
func (s *Service) Delete(ctx context.Context, clientCert *ClientCert) error {
err := s.client.Delete(ctx, clientCert.CertConfig)
if apierrors.IsNotFound(err) {
// Resource was already deleted.
} else if err != nil {
return microerror.Mask(err)
}
return nil
}
func (s *Service) GetCredential(ctx context.Context, namespace, name string) (*corev1.Secret, error) {
secret := &corev1.Secret{}
err := s.client.Get(ctx, client.ObjectKey{Name: name, Namespace: namespace}, secret)
if apierrors.IsNotFound(err) {
return nil, microerror.Mask(notFoundError)
} else if err != nil {
return nil, microerror.Mask(err)
}
return secret, nil
}