forked from oVirt/cluster-api-provider-ovirt
/
common.go
76 lines (66 loc) · 1.96 KB
/
common.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
package ovirt
import (
"fmt"
"time"
"github.com/go-logr/logr"
"github.com/openshift/cluster-api-provider-ovirt/pkg/utils"
ovirtclient "github.com/ovirt/go-ovirt-client"
kloglogger "github.com/ovirt/go-ovirt-client-log-klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
const requeueDefaultTime = 30 * time.Second
func ResultRequeueAfter(sec int) reconcile.Result {
return reconcile.Result{RequeueAfter: time.Duration(sec) * time.Second}
}
func ResultRequeueDefault() reconcile.Result {
return reconcile.Result{RequeueAfter: requeueDefaultTime}
}
func ResultNoRequeue() reconcile.Result {
return reconcile.Result{Requeue: false}
}
type BaseController struct {
Log logr.Logger
Client client.Client
ovirtClient ovirtclient.Client
}
// GetoVirtClient returns a a client to oVirt's API endpoint
func (b *BaseController) GetoVirtClient() (ovirtclient.Client, error) {
if b.ovirtClient == nil || b.ovirtClient.Test() != nil {
var err error
// session expired or some other error, re-login.
b.ovirtClient, err = GetoVirtClient(b.Client)
if err != nil {
return nil, fmt.Errorf("failed creating ovirt connection %w", err)
}
}
return b.ovirtClient, nil
}
// GetoVirtClient returns a ovirt client by using the credentials from the K8 secrets
func GetoVirtClient(k8sClient client.Client) (ovirtclient.Client, error) {
creds, err := GetCredentialsSecret(k8sClient, utils.NAMESPACE, utils.OvirtCloudCredsSecretName)
if err != nil {
return nil, fmt.Errorf("failed getting credentials for namespace %s, %w", utils.NAMESPACE, err)
}
tls := ovirtclient.TLS()
if creds.Insecure {
tls.Insecure()
} else {
if creds.CAFile != "" {
tls.CACertsFromFile(creds.CAFile)
}
if creds.CABundle != "" {
tls.CACertsFromMemory([]byte(creds.CABundle))
}
tls.CACertsFromSystem()
}
logger := kloglogger.New()
return ovirtclient.New(
creds.URL,
creds.Username,
creds.Password,
tls,
logger,
nil,
)
}