add timeout for unabailable kubernetes server

reviewable/pr2585/r1
Wil Reichert 8 years ago
parent a6c9b74a63
commit cbc16277c9

@ -19,9 +19,12 @@ package rules
import ( import (
"errors" "errors"
"fmt" "fmt"
"net"
"net/url"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"time"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
"k8s.io/apimachinery/pkg/version" "k8s.io/apimachinery/pkg/version"
@ -81,6 +84,8 @@ func Templates(linter *support.Linter) {
return return
} }
serverAvailable := true
/* Iterate over all the templates to check: /* Iterate over all the templates to check:
- It is a .yaml file - It is a .yaml file
- All the values in the template file is defined - All the values in the template file is defined
@ -88,6 +93,7 @@ func Templates(linter *support.Linter) {
- Generated content is a valid Yaml file - Generated content is a valid Yaml file
- Metadata.Namespace is not set - Metadata.Namespace is not set
*/ */
for _, template := range chart.Templates { for _, template := range chart.Templates {
fileName, _ := template.Name, template.Data fileName, _ := template.Name, template.Data
path = fileName path = fileName
@ -118,8 +124,24 @@ func Templates(linter *support.Linter) {
continue continue
} }
// access kubectl client if serverAvailable {
kubeClient := kube.New(kube.GetConfig("")) // access kubernetes URL from the kubectl client
kubeConfig := kube.GetConfig("")
clientConfig, _ := kubeConfig.ClientConfig()
u, _ := url.Parse(clientConfig.Host)
// if kubernetes server is unavailable print a warning
// and don't try again this run.
timeout := time.Duration(5 * time.Second)
_, err = net.DialTimeout("tcp" , u.Host , timeout)
if err != nil {
e := fmt.Errorf("%s, skipping schema validation\n", err)
linter.RunLinterRule(support.WarningSev, path, e)
serverAvailable = false
continue
}
kubeClient := kube.New(kubeConfig)
f := kubeClient.Factory f := kubeClient.Factory
// get the schema validator // get the schema validator
@ -132,15 +154,15 @@ func Templates(linter *support.Linter) {
// convert to YAML to JSON, validated above so should be ok // convert to YAML to JSON, validated above so should be ok
j, _ := yaml.YAMLToJSON([]byte(renderedContent)) j, _ := yaml.YAMLToJSON([]byte(renderedContent))
//
err = schema.ValidateBytes(j) err = schema.ValidateBytes(j)
validSchema := linter.RunLinterRule(support.ErrorSev, path, validateSchema(err)) validSchema := linter.RunLinterRule(support.ErrorSev, path, validateSchema(err))
if !validSchema { if !validSchema {
continue continue
} }
} }
}
} }
// Validation functions // Validation functions

Loading…
Cancel
Save