-
Notifications
You must be signed in to change notification settings - Fork 36
/
operator_install.go
68 lines (55 loc) · 2.14 KB
/
operator_install.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
package action
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
olmv1 "github.com/operator-framework/operator-controller/api/v1alpha1"
"github.com/operator-framework/kubectl-operator/pkg/action"
)
type OperatorInstall struct {
config *action.Configuration
Package string
Logf func(string, ...interface{})
}
func NewOperatorInstall(cfg *action.Configuration) *OperatorInstall {
return &OperatorInstall{
config: cfg,
Logf: func(string, ...interface{}) {},
}
}
func (i *OperatorInstall) Run(ctx context.Context) (*olmv1.ClusterExtension, error) {
// TODO(developer): Lookup package information when the OLMv1 equivalent of the
// packagemanifests API is available. That way, we can check to see if the
// package is actually available to the cluster before creating the Operator
// object.
opKey := types.NamespacedName{Name: i.Package}
op := &olmv1.ClusterExtension{
ObjectMeta: metav1.ObjectMeta{Name: opKey.Name},
Spec: olmv1.ClusterExtensionSpec{PackageName: i.Package},
}
if err := i.config.Client.Create(ctx, op); err != nil {
return nil, err
}
// TODO(developer): Improve the logic in this poll wait once the Operator reconciler
// and conditions types and reasons are improved. For now, this will stop waiting as
// soon as a Ready condition is found, but we should probably wait until the Operator
// stops progressing.
// All Types will exist, so Ready may have a false Status. So, wait until
// Type=Ready,Status=True happens
if err := wait.PollUntilContextCancel(ctx, pollTimeout, true, func(conditionCtx context.Context) (bool, error) {
if err := i.config.Client.Get(conditionCtx, opKey, op); err != nil {
return false, err
}
installedCondition := meta.FindStatusCondition(op.Status.Conditions, olmv1.TypeInstalled)
if installedCondition != nil && installedCondition.Status == metav1.ConditionTrue {
return true, nil
}
return false, nil
}); err != nil {
return nil, fmt.Errorf("waiting for operator to become ready: %v", err)
}
return op, nil
}