-
Notifications
You must be signed in to change notification settings - Fork 114
/
error.go
138 lines (108 loc) · 3.93 KB
/
error.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
// Copyright 2021, Pulumi Corporation. All rights reserved.
package await
import (
"fmt"
"strings"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
// AggregatedError represents an error with 0 or more sub-errors.
type AggregatedError interface {
SubErrors() []string
}
// PartialError represents an object that failed to complete its current operation.
type PartialError interface {
Object() *unstructured.Unstructured
}
// PreviewError represents a preview operation that failed.
type PreviewError interface {
Object() *unstructured.Unstructured
}
// cancellationError represents an operation that failed because the user cancelled it.
type cancellationError struct {
object *unstructured.Unstructured
subErrors []string
}
var _ error = (*cancellationError)(nil)
var _ AggregatedError = (*cancellationError)(nil)
var _ PartialError = (*cancellationError)(nil)
func (ce *cancellationError) Error() string {
return fmt.Sprintf("Resource operation was cancelled for %q", ce.object.GetName())
}
// SubErrors returns the errors that were present when cancellation occurred.
func (ce *cancellationError) SubErrors() []string {
return ce.subErrors
}
func (ce *cancellationError) Object() *unstructured.Unstructured {
return ce.object
}
// namespaceError represents an operation that failed because the namespace didn't exist.
type namespaceError struct {
object *unstructured.Unstructured
}
var _ error = (*namespaceError)(nil)
var _ PreviewError = (*namespaceError)(nil)
func (ne *namespaceError) Error() string {
return fmt.Sprintf("namespace does not exist for %q", ne.object.GetName())
}
func (ne *namespaceError) Object() *unstructured.Unstructured {
return ne.object
}
// timeoutError represents an operation that failed because it timed out.
type timeoutError struct {
object *unstructured.Unstructured
subErrors []string
}
var _ error = (*timeoutError)(nil)
var _ AggregatedError = (*timeoutError)(nil)
var _ PartialError = (*timeoutError)(nil)
func (te *timeoutError) Error() string {
// TODO(levi): May want to add a shortlink to more detailed troubleshooting docs.
return fmt.Sprintf("'%s' timed out waiting to be Ready", te.object.GetName())
}
// SubErrors returns the errors that were present when timeout occurred.
func (te *timeoutError) SubErrors() []string {
return te.subErrors
}
func (te *timeoutError) Object() *unstructured.Unstructured {
return te.object
}
// initializationError occurs when we attempt to read a resource that failed to fully initialize.
type initializationError struct {
subErrors []string
object *unstructured.Unstructured
}
var _ error = (*initializationError)(nil)
var _ AggregatedError = (*initializationError)(nil)
var _ PartialError = (*initializationError)(nil)
func (ie *initializationError) Error() string {
return fmt.Sprintf("Resource '%s' was created but failed to initialize", ie.object.GetName())
}
// SubErrors returns the errors that were present when timeout occurred.
func (ie *initializationError) SubErrors() []string {
return ie.subErrors
}
func (ie *initializationError) Object() *unstructured.Unstructured {
return ie.object
}
// IsNamespaceNotFoundErr returns true if the namespace wasn't found for a k8s client operation.
func IsNamespaceNotFoundErr(err error) bool {
se, isStatusError := err.(*errors.StatusError)
if !isStatusError {
return false
}
return errors.IsNotFound(err) && se.Status().Details.Kind == "namespaces"
}
// IsResourceExistsErr returns true if the resource already exists on the k8s cluster.
func IsResourceExistsErr(err error) bool {
_, isStatusError := err.(*errors.StatusError)
if !isStatusError {
return false
}
return errors.IsAlreadyExists(err)
}
// IsDeleteRequiredFieldErr is true if the user attempted to delete a Patch resource that was the sole manager of a
// required field.
func IsDeleteRequiredFieldErr(err error) bool {
return errors.IsInvalid(err) && strings.Contains(err.Error(), "Required value")
}