Skip to content

Commit

Permalink
Merge pull request #433 from calmkart/notfound
Browse files Browse the repository at this point in the history
MOD:change string contain 'not found' to k8s error
  • Loading branch information
jonnydawg committed Apr 23, 2020
2 parents 4db8d7e + ff6e7e0 commit f506187
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
4 changes: 2 additions & 2 deletions cmd/deployment-check/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
"context"
"errors"
"fmt"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
"math"
"strconv"
"strings"
"time"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -609,7 +609,7 @@ func waitForDeploymentToDelete() chan bool {
_, err := client.AppsV1().Deployments(checkNamespace).Get(checkDeploymentName, metav1.GetOptions{})
if err != nil {
log.Debugln("error from Deployments().Get():", err.Error())
if strings.Contains(err.Error(), "not found") {
if k8sErrors.IsNotFound(err) {
log.Debugln("Deployment deleted.")
deleteChan <- true
return
Expand Down
4 changes: 2 additions & 2 deletions cmd/deployment-check/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"context"
"errors"
"fmt"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
"strconv"
"strings"
"time"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -393,7 +393,7 @@ func waitForServiceToDelete() chan bool {
_, err := client.CoreV1().Services(checkNamespace).Get(checkServiceName, metav1.GetOptions{})
if err != nil {
log.Debugln("error from Services().Get():", err.Error())
if strings.Contains(err.Error(), "not found") {
if k8sErrors.IsNotFound(err) {
log.Debugln("Service deleted.")
deleteChan <- true
return
Expand Down
3 changes: 2 additions & 1 deletion cmd/kuberhealthy/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package main

import (
"errors"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
"strings"
"time"

Expand Down Expand Up @@ -69,7 +70,7 @@ func ensureStateResourceExists(checkName string, checkNamespace string) error {
log.Debugln("Checking existence of custom resource:", name)
state, err := khStateClient.Get(metav1.GetOptions{}, stateCRDResource, name, checkNamespace)
if err != nil {
if strings.Contains(err.Error(), "not found") {
if k8sErrors.IsNotFound(err) {
log.Infoln("Custom resource not found, creating resource:", name, " - ", err)
initialDetails := health.NewCheckDetails()
initialState := khstatecrd.NewKuberhealthyState(name, initialDetails)
Expand Down
4 changes: 2 additions & 2 deletions cmd/pod-restarts-check/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"time"

log "github.com/sirupsen/logrus"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"

checkclient "github.com/Comcast/kuberhealthy/v2/pkg/checks/external/checkclient"
"github.com/Comcast/kuberhealthy/v2/pkg/kubeClient"
Expand Down Expand Up @@ -191,7 +191,7 @@ func (prc *Checker) verifyBadPodRestartExists(podName string) error {

_, err := prc.client.CoreV1().Pods(prc.Namespace).Get(podName, metav1.GetOptions{})
if err != nil {
if strings.Contains(err.Error(), "not found") {
if k8sErrors.IsNotFound(err) {
log.Infoln("Bad Pod:", podName, "no longer exists. Removing from bad pods map")
delete(prc.BadPods, podName)
} else {
Expand Down
15 changes: 8 additions & 7 deletions pkg/checks/external/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"errors"
"fmt"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -156,7 +157,7 @@ func (ext *Checker) CurrentStatus() (bool, []string) {
// fetch the state from the resource
state, err := ext.getKHState()
if err != nil {
if strings.Contains(err.Error(), "not found") {
if k8sErrors.IsNotFound(err) {
// if the resource is not found, we default to "up" so not to throw alarms before the first run completes
return true, []string{}
}
Expand Down Expand Up @@ -294,12 +295,12 @@ func (ext *Checker) setUUID(uuid string) error {
checkState, err := ext.getKHState()

// if the fetch operation had an error, but it wasn't 'not found', we return here
if err != nil && !strings.Contains(err.Error(), "not found") {
if err != nil && !k8sErrors.IsNotFound(err) {
return fmt.Errorf("error setting uuid for check %s %w", ext.CheckName, err)
}

// if the check was not found, we create a fresh one and start there
if err != nil && strings.Contains(err.Error(), "not found") {
if err != nil && k8sErrors.IsNotFound(err) {
ext.log("khstate did not exist, so a default object will be created")
details := health.NewCheckDetails()
details.Namespace = ext.CheckNamespace()
Expand Down Expand Up @@ -703,7 +704,7 @@ func (ext *Checker) deletePod(podName string) error {
GracePeriodSeconds: &gracePeriodSeconds,
PropagationPolicy: &deletionPolicy,
})
if err != nil && !strings.Contains(err.Error(), "not found") {
if err != nil && !k8sErrors.IsNotFound(err) {
return err
}
return nil
Expand Down Expand Up @@ -738,7 +739,7 @@ func (ext *Checker) getCheckLastUpdateTime() (time.Time, error) {

// fetch the state from the resource
state, err := ext.getKHState()
if err != nil && strings.Contains(err.Error(), "not found") {
if err != nil && k8sErrors.IsNotFound(err) {
return time.Time{}, nil
}

Expand Down Expand Up @@ -847,7 +848,7 @@ func (ext *Checker) waitForAllPodsToClear() chan error {

// if we got a "not found" message, then we are done. This is the happy path.
if err != nil {
if strings.Contains(err.Error(), "not found") {
if k8sErrors.IsNotFound(err) {
ext.log("all pods cleared")
outChan <- nil
return
Expand Down Expand Up @@ -1167,7 +1168,7 @@ func (ext *Checker) podExists() (bool, error) {

// if the pod is "not found", then it does not exist
p, err := podClient.Get(ext.podName(), metav1.GetOptions{})
if err != nil && strings.Contains(err.Error(), "not found") {
if err != nil && k8sErrors.IsNotFound(err) {
return false, nil
}

Expand Down

0 comments on commit f506187

Please sign in to comment.