Skip to content

Commit 63fed39

Browse files
tfadeyioluwole.fadeyiJammyL
authored
Add strict flag to agent (#196)
* Add strict flag to agent Agent will hard fail if a data gatherer is defined for a missing CRD. Related [#510](jetstack/preflight-platform#510) Signed-off-by: oluwole.fadeyi <[email protected]> * Adds new type of error and changed strict flag New error type: CRDNotFoundError This is to catch the edge case that when a CRD is not found the backoff loop should not be retried The strict flag has been changed to hard fail if an error of any type is returned, including if a CRD is not found Signed-off-by: Jamie Leppard <[email protected]> * Improves error handling Error from CRDNotFoundError is now not appeneded to dgError if not in strict mode. Strict mode no longer iterates over a multi error but checks to see if any errors are present at all Signed-off-by: Jamie Leppard <[email protected]> * Changes CRDNotFoundError to ConfigError The CRDNotFound error in the k8s package has been changed to a more general ConfigError in the local package Signed-off-by: Jamie Leppard <[email protected]> * Added new error package for datagatherers The config error has been moved to a new package dedicated to error associated with data gatherers Signed-off-by: Jamie Leppard <[email protected]> Co-authored-by: oluwole.fadeyi <[email protected]> Co-authored-by: Jamie Leppard <[email protected]>
1 parent c62dbc4 commit 63fed39

File tree

4 files changed

+57
-8
lines changed

4 files changed

+57
-8
lines changed

cmd/agent.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,12 @@ func init() {
7979
10*time.Minute,
8080
"Max time for retrying failed data gatherers (given as XhYmZs).",
8181
)
82+
agentCmd.PersistentFlags().BoolVarP(
83+
&agent.StrictMode,
84+
"strict",
85+
"",
86+
false,
87+
"Runs agent in strict mode. No retry attempts will be made for a missing data gatherer's data.",
88+
)
8289

8390
}

pkg/agent/run.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/jetstack/preflight/api"
1818
"github.com/jetstack/preflight/pkg/client"
1919
"github.com/jetstack/preflight/pkg/datagatherer"
20+
dgerror "github.com/jetstack/preflight/pkg/datagatherer/error"
2021
"github.com/jetstack/preflight/pkg/version"
2122
"github.com/spf13/cobra"
2223
)
@@ -42,6 +43,9 @@ var InputPath string
4243
// BackoffMaxTime is the maximum time for which data gatherers will be retried
4344
var BackoffMaxTime time.Duration
4445

46+
// StrictMode flag causes the agent to fail at the first attempt
47+
var StrictMode bool
48+
4549
// Run starts the agent process
4650
func Run(cmd *cobra.Command, args []string) {
4751
ctx := context.Background()
@@ -207,8 +211,17 @@ func gatherData(ctx context.Context, config Config) []*api.DataReading {
207211
}
208212
dgData, err := dg.Fetch()
209213
if err != nil {
210-
err = fmt.Errorf("%s: %v", k, err)
211-
dgError = multierror.Append(dgError, err)
214+
if _, ok := err.(*dgerror.ConfigError); ok {
215+
if StrictMode {
216+
err = fmt.Errorf("%s: %v", k, err)
217+
dgError = multierror.Append(dgError, err)
218+
} else {
219+
log.Printf("%s: %v", k, err)
220+
}
221+
} else {
222+
err = fmt.Errorf("%s: %v", k, err)
223+
dgError = multierror.Append(dgError, err)
224+
}
212225
continue
213226
} else {
214227
completedDataGatherers[k] = true
@@ -238,13 +251,21 @@ func gatherData(ctx context.Context, config Config) []*api.DataReading {
238251
return dgError.ErrorOrNil()
239252
}
240253

241-
err := backoff.RetryNotify(getReadings, backOff, notify)
242-
if err != nil {
243-
log.Println(err)
244-
log.Printf("This will not be retried")
254+
if StrictMode {
255+
err := getReadings()
256+
if err != nil {
257+
log.Fatalf("%v", err)
258+
}
245259
} else {
246-
log.Printf("All data gatherers successfull")
260+
err := backoff.RetryNotify(func() error { return getReadings() }, backOff, notify)
261+
if err != nil {
262+
log.Println(err)
263+
log.Printf("This will not be retried")
264+
} else {
265+
log.Printf("Finished gathering data")
266+
}
247267
}
268+
248269
return readings
249270
}
250271

pkg/datagatherer/error/error.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package dgerror
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// ConfigError is the error type for a misconfiguration. e.g. A missing CRD
8+
type ConfigError struct {
9+
Err string
10+
}
11+
12+
func (e *ConfigError) Error() string {
13+
return fmt.Sprintf("%s", e.Err)
14+
}

pkg/datagatherer/k8s/dynamic.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"strings"
77

88
"github.com/jetstack/preflight/pkg/datagatherer"
9+
dgerror "github.com/jetstack/preflight/pkg/datagatherer/error"
910
"github.com/pkg/errors"
11+
statusError "k8s.io/apimachinery/pkg/api/errors"
1012
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1113
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1214
"k8s.io/apimachinery/pkg/fields"
@@ -139,7 +141,12 @@ func (g *DataGathererDynamic) Fetch() (interface{}, error) {
139141
FieldSelector: g.fieldSelector,
140142
})
141143
if err != nil {
142-
return nil, errors.WithStack(err)
144+
if statusErr, ok := err.(*statusError.StatusError); ok {
145+
if statusErr.Status().Code == 404 {
146+
return nil, &dgerror.ConfigError{Err: err.Error()}
147+
}
148+
}
149+
return nil, err
143150
}
144151
list.Object = namespaceList.Object
145152
list.Items = append(list.Items, namespaceList.Items...)

0 commit comments

Comments
 (0)