Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MOD:change string contain 'not found' to k8s error #433

Merged
merged 1 commit into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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