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,27 +124,43 @@ func Templates(linter *support.Linter) {
continue continue
} }
// access kubectl client if serverAvailable {
kubeClient := kube.New(kube.GetConfig("")) // access kubernetes URL from the kubectl client
f := kubeClient.Factory kubeConfig := kube.GetConfig("")
clientConfig, _ := kubeConfig.ClientConfig()
// get the schema validator u, _ := url.Parse(clientConfig.Host)
schema, err := f.Validator(true, kubeClient.SchemaCacheDir)
validSchemaAccess := linter.RunLinterRule(support.ErrorSev, path, validateSchemaAccess(err)) // if kubernetes server is unavailable print a warning
// and don't try again this run.
if !validSchemaAccess { timeout := time.Duration(5 * time.Second)
continue _, err = net.DialTimeout("tcp" , u.Host , timeout)
} if err != nil {
e := fmt.Errorf("%s, skipping schema validation\n", err)
// convert to YAML to JSON, validated above so should be ok linter.RunLinterRule(support.WarningSev, path, e)
j, _ := yaml.YAMLToJSON([]byte(renderedContent)) serverAvailable = false
continue
// }
err = schema.ValidateBytes(j)
validSchema := linter.RunLinterRule(support.ErrorSev, path, validateSchema(err)) kubeClient := kube.New(kubeConfig)
f := kubeClient.Factory
if !validSchema {
continue // 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