mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-04 15:05:20 +00:00
Address comments
This commit is contained in:
@@ -17,6 +17,7 @@ limitations under the License.
|
||||
package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -78,7 +79,7 @@ type Factory struct {
|
||||
// LabelsForObject returns the labels associated with the provided object
|
||||
LabelsForObject func(object runtime.Object) (map[string]string, error)
|
||||
// Returns a schema that can validate objects stored on disk.
|
||||
Validator func(validate, cacheSchema bool) (validation.Schema, error)
|
||||
Validator func(validate bool, cacheDir string) (validation.Schema, error)
|
||||
// Returns the default namespace to use in cases where no
|
||||
// other namespace is specified and whether the namespace was
|
||||
// overriden.
|
||||
@@ -216,17 +217,25 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
|
||||
}
|
||||
return kubectl.ReaperFor(mapping.Kind, client)
|
||||
},
|
||||
Validator: func(validate, cacheSchema bool) (validation.Schema, error) {
|
||||
Validator: func(validate bool, cacheDir string) (validation.Schema, error) {
|
||||
if validate {
|
||||
client, err := clients.ClientForVersion("")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var cacheDir string
|
||||
if cacheSchema {
|
||||
cacheDir = "/tmp"
|
||||
dir := cacheDir
|
||||
if len(dir) > 0 {
|
||||
version, err := client.ServerVersion()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dir = path.Join(cacheDir, version.String())
|
||||
}
|
||||
return &clientSwaggerSchema{client, client.ExperimentalClient, cacheDir}, nil
|
||||
return &clientSwaggerSchema{
|
||||
c: client,
|
||||
ec: client.ExperimentalClient,
|
||||
cacheDir: dir,
|
||||
}, nil
|
||||
}
|
||||
return validation.NullSchema{}, nil
|
||||
},
|
||||
@@ -311,7 +320,14 @@ func getSchemaAndValidate(c schemaClient, data []byte, group, version, cacheDir
|
||||
if err = os.MkdirAll(path.Join(cacheDir, group, version), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = ioutil.WriteFile(cacheFile, schemaData, 0644); err != nil {
|
||||
tmpFile, err := ioutil.TempFile(cacheDir, "schema")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := io.Copy(tmpFile, bytes.NewBuffer(schemaData)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Link(tmpFile.Name(), cacheFile); err != nil && !os.IsExist(err) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user