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 (
"errors"
"fmt"
"net"
"net/url"
"os"
"path/filepath"
"runtime"
"time"
"github.com/ghodss/yaml"
"k8s.io/apimachinery/pkg/version"
@ -81,6 +84,8 @@ func Templates(linter *support.Linter) {
return
}
serverAvailable := true
/* Iterate over all the templates to check:
- It is a .yaml file
- 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
- Metadata.Namespace is not set
*/
for _, template := range chart.Templates {
fileName, _ := template.Name, template.Data
path = fileName
@ -118,27 +124,43 @@ func Templates(linter *support.Linter) {
continue
}
// access kubectl client
kubeClient := kube.New(kube.GetConfig(""))
f := kubeClient.Factory
// get the schema validator
schema, err := f.Validator(true, kubeClient.SchemaCacheDir)
validSchemaAccess := linter.RunLinterRule(support.ErrorSev, path, validateSchemaAccess(err))
if !validSchemaAccess {
continue
}
// convert to YAML to JSON, validated above so should be ok
j, _ := yaml.YAMLToJSON([]byte(renderedContent))
//
err = schema.ValidateBytes(j)
validSchema := linter.RunLinterRule(support.ErrorSev, path, validateSchema(err))
if !validSchema {
continue
if serverAvailable {
// 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
// get the schema validator
schema, err := f.Validator(true, kubeClient.SchemaCacheDir)
validSchemaAccess := linter.RunLinterRule(support.ErrorSev, path, validateSchemaAccess(err))
if !validSchemaAccess {
continue
}
// convert to YAML to JSON, validated above so should be ok
j, _ := yaml.YAMLToJSON([]byte(renderedContent))
err = schema.ValidateBytes(j)
validSchema := linter.RunLinterRule(support.ErrorSev, path, validateSchema(err))
if !validSchema {
continue
}
}
}
}

Loading…
Cancel
Save