-
Notifications
You must be signed in to change notification settings - Fork 8
/
api.go
147 lines (119 loc) · 4.11 KB
/
api.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
Copyright 2021 The cert-manager Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package api
import (
"context"
"errors"
"fmt"
"time"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
logf "github.com/cert-manager/cert-manager/pkg/logs"
"github.com/cert-manager/cert-manager/pkg/util/cmapichecker"
cmcmdutil "github.com/cert-manager/cmctl/v2/internal/util"
"github.com/cert-manager/cmctl/v2/pkg/factory"
)
// Options is a struct to support check api command
type Options struct {
// APIChecker is used to check that the cert-manager CRDs have been installed on the K8S
// API server and that the cert-manager webhooks are all working
APIChecker cmapichecker.Interface
// Time before timeout when waiting
Wait time.Duration
// Time between checks when waiting
Interval time.Duration
genericclioptions.IOStreams
*factory.Factory
}
var checkApiDesc = templates.LongDesc(i18n.T(`
This check attempts to perform a dry-run create of a cert-manager *v1alpha2*
Certificate resource in order to verify that CRDs are installed and all the
required webhooks are reachable by the K8S API server.
We use v1alpha2 API to ensure that the API server has also connected to the
cert-manager conversion webhook.`))
// NewOptions returns initialized Options
func NewOptions(ioStreams genericclioptions.IOStreams) *Options {
return &Options{
IOStreams: ioStreams,
}
}
// Complete takes the command arguments and factory and infers any remaining options.
func (o *Options) Complete() error {
var err error
o.APIChecker, err = cmapichecker.New(
o.RESTConfig,
runtime.NewScheme(),
o.Namespace,
)
if err != nil {
return err
}
return nil
}
// NewCmdCheckApi returns a cobra command for checking creating cert-manager resources against the K8S API server
func NewCmdCheckApi(setupCtx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command {
o := NewOptions(ioStreams)
cmd := &cobra.Command{
Use: "api",
Short: "Check if the cert-manager API is ready",
Long: checkApiDesc,
PreRunE: func(cmd *cobra.Command, args []string) error {
return o.Complete()
},
RunE: func(cmd *cobra.Command, args []string) error {
return o.Run(cmd.Context())
},
}
cmd.Flags().DurationVar(&o.Wait, "wait", 0, "Wait until the cert-manager API is ready (default 0s = poll once)")
cmd.Flags().DurationVar(&o.Interval, "interval", 5*time.Second, "Time between checks when waiting, must include unit, e.g. 1m or 10m")
o.Factory = factory.New(cmd)
return cmd
}
// Run executes check api command
func (o *Options) Run(ctx context.Context) error {
log := logf.FromContext(ctx, "checkAPI")
start := time.Now()
var lastError error
pollErr := wait.PollUntilContextCancel(ctx, o.Interval, true, func(ctx context.Context) (bool, error) {
if err := o.APIChecker.Check(ctx); err != nil {
simpleError := cmapichecker.TranslateToSimpleError(err)
if simpleError != nil {
log.V(2).Info("Not ready", "err", simpleError, "underlyingError", err)
lastError = simpleError
} else {
log.V(2).Info("Not ready", "err", err)
lastError = err
}
if time.Since(start) > o.Wait {
return false, context.DeadlineExceeded
}
return false, nil
}
return true, nil
})
if pollErr != nil {
if errors.Is(pollErr, context.DeadlineExceeded) && o.Wait > 0 {
log.V(2).Info("Timed out", "after", o.Wait, "err", pollErr)
cmcmdutil.SetExitCode(pollErr)
} else {
cmcmdutil.SetExitCode(lastError)
}
return lastError
}
fmt.Fprintln(o.Out, "The cert-manager API is ready")
return nil
}