/
regctl.go
54 lines (43 loc) · 1.13 KB
/
regctl.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
package signctl
import (
"context"
"strings"
regv1 "github.com/tmax-cloud/registry-operator/api/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// NewRegCtl is a controller for registry
// if registryName or registryNamespace is empty string, RegCtl is nil
func NewRegCtl(c client.Client, regName, namespace string) *RegCtl {
if len(regName) == 0 || len(namespace) == 0 {
return nil
}
reg, err := getRegistry(c, regName, namespace)
if err != nil {
return nil
}
return &RegCtl{
client: c,
reg: reg,
}
}
type RegCtl struct {
client client.Client
reg *regv1.Registry
}
func (r *RegCtl) GetHostname() string {
return strings.TrimPrefix(r.GetEndpoint(), "https://")
}
func (r *RegCtl) GetEndpoint() string {
return r.reg.Status.ServerURL
}
func (r *RegCtl) GetNotaryEndpoint() string {
return r.reg.Status.NotaryURL
}
func getRegistry(c client.Client, regName, namespace string) (*regv1.Registry, error) {
reg := ®v1.Registry{}
if err := c.Get(context.TODO(), types.NamespacedName{Name: regName, Namespace: namespace}, reg); err != nil {
return nil, err
}
return reg, nil
}