Skip to content

Commit

Permalink
Add minor check for buildnodes and implement better output
Browse files Browse the repository at this point in the history
  • Loading branch information
lbischof committed Aug 23, 2018
1 parent 48f48c9 commit 56466f9
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 43 deletions.
26 changes: 13 additions & 13 deletions daemon/client/checks/certificates.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
package checks

import (
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"net/http"
"os"
"io/ioutil"
"fmt"
"path/filepath"
"crypto/x509"
"time"
"gopkg.in/yaml.v2"
"encoding/base64"
"errors"
"net/http"
"crypto/tls"
)

type Cert struct {
File string
File string
DaysLeft int
}

type KubeConfig struct {
APIVersion string `yaml:"apiVersion"`
Clusters []struct {
Clusters []struct {
Cluster struct {
CertificateAuthorityData string `yaml:"certificate-authority-data"`
Server string `yaml:"server"`
Expand All @@ -40,7 +40,7 @@ type KubeConfig struct {
} `yaml:"contexts"`
CurrentContext string `yaml:"current-context"`
Kind string `yaml:"kind"`
Preferences struct {
Preferences struct {
} `yaml:"preferences"`
Users []struct {
Name string `yaml:"name"`
Expand Down Expand Up @@ -122,7 +122,7 @@ func CheckKubeSslCertificates(kubePaths []string, days int) error {

err = yaml.Unmarshal(data, &kubeConfig)
if err != nil {
msg := fmt.Sprintf("unmarshalling %s failed (%s)", kubeFile, err.Error())
msg := fmt.Sprintf("unmarshalling %s failed (%s)", kubeFile, err.Error())
log.Println(msg)
return errors.New(msg)
}
Expand Down Expand Up @@ -310,7 +310,7 @@ func getExpiredCerts(filePaths []string, days int) (error, []Cert) {

if int(daysLeft) <= days {
log.Println(fmt.Sprintf("%s expires in %d days", file, int(daysLeft)))
expiredCerts = append(expiredCerts, Cert { File: file, DaysLeft: int(daysLeft) })
expiredCerts = append(expiredCerts, Cert{File: file, DaysLeft: int(daysLeft)})
}
}
}
Expand Down
58 changes: 34 additions & 24 deletions daemon/client/checks/openshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,41 +33,51 @@ func CheckMasterApis(urls string) error {
}
}

func CheckOcGetNodes() error {
func CheckOcGetNodes(buildNodes bool) error {
log.Println("Checking oc get nodes output")

out, err := runOcGetNodes()
if err != nil {
return err
}

if strings.Contains(out, "NotReady") {
// Wait a few seconds and see if still NotReady
// to avoid wrong alerts
time.Sleep(10 * time.Second)

out2, err := runOcGetNodes()
var out string
var err error
for i := 0; i < 5; i++ {
out, err = runOcGetNodes(buildNodes)
if err != nil {
return err
}
if strings.Contains(out2, "NotReady") {
if strings.Contains(out, "NotReady") {
// Wait a few seconds and see if still NotReady
// to avoid wrong alerts
time.Sleep(10 * time.Second)

out3, err := runOcGetNodes()
if err != nil {
return err
}
if strings.Contains(out3, "NotReady") {
return errors.New("Some node is not ready! 'oc get nodes' output contained NotReady. Output: " + out3)
}
continue
}
return nil
}
var purpose string
if buildNodes {
purpose = "Buildnode "
} else {
purpose = "Workernode "
}
return errors.New(purpose + getNotReadyNodeNames(out) + " is not ready! 'oc get nodes' output contained NotReady. Output: " + out)
}

return nil
func getNotReadyNodeNames(out string) string {
lines := strings.Split(out, "\n")
var notReadyNodes []string
for _, line := range lines {
if strings.Contains(line, "NotReady") {
s := strings.Fields(line)[0]
notReadyNodes = append(notReadyNodes, s)
}
}
return strings.Join(notReadyNodes, ", ")
}

func runOcGetNodes() (string, error) {
out, err := exec.Command("bash", "-c", "oc get nodes --show-labels | grep -v monitoring=false | grep -v purpose=buildnode | grep -v SchedulingDisabled").Output()
func runOcGetNodes(buildNodes bool) (string, error) {
buildNodes_grep_params := "-v"
if buildNodes {
buildNodes_grep_params = ""
}
out, err := exec.Command("bash", "-c", fmt.Sprintf("oc get nodes --show-labels | grep -v monitoring=false | grep %s purpose=buildnode | grep -v SchedulingDisabled", buildNodes_grep_params)).Output()
if err != nil {
msg := "Could not parse oc get nodes output: " + err.Error()
log.Println(msg)
Expand Down
4 changes: 2 additions & 2 deletions daemon/client/checks/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"errors"
"fmt"
"log"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
"os"
"regexp"
)

func CheckOpenFileCount() error {
Expand Down
2 changes: 1 addition & 1 deletion daemon/client/handlers/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ func generateResponse(w http.ResponseWriter, errors []string) {

w.Header().Set("Content-Type", "application/json")
w.Write(json)
}
}
6 changes: 4 additions & 2 deletions daemon/client/handlers/major.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ func HandleMajorChecks(daemonType string, w http.ResponseWriter, r *http.Request
log.Fatal("env variables 'ETCD_IPS', 'REGISTRY_SVC_IP', 'ROUTER_IPS', 'CHECK_CERTIFICATE_URLS' must be specified on type 'MASTER'")
}

if err := checks.CheckOcGetNodes(); err != nil {
// boolean false means exclude buildnodes
// boolean true means only buildnodes
if err := checks.CheckOcGetNodes(false); err != nil {
errors = append(errors, err.Error())
}

Expand Down Expand Up @@ -109,4 +111,4 @@ func HandleMajorChecks(daemonType string, w http.ResponseWriter, r *http.Request
}

generateResponse(w, errors)
}
}
6 changes: 6 additions & 0 deletions daemon/client/handlers/minor.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ func HandleMinorChecks(daemonType string, w http.ResponseWriter, r *http.Request
log.Fatal("allowedWithout seems not to be an integer", allowedWithout)
}

// boolean false means exclude buildnodes
// boolean true means only buildnodes
if err := checks.CheckOcGetNodes(true); err != nil {
errors = append(errors, err.Error())
}

if err := checks.CheckExternalSystem(externalSystem); err != nil {
errors = append(errors, err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion daemon/client/webserver.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package client

import (
"github.com/oscp/openshift-monitoring/daemon/client/handlers"
"log"
"net/http"
"os"
"github.com/oscp/openshift-monitoring/daemon/client/handlers"
)

func RunWebserver(daemonType string) {
Expand Down

0 comments on commit 56466f9

Please sign in to comment.