// SwaggerSchema retrieves and parses the swagger API schema the server supports. func (c *Client) SwaggerSchema(version string) (*swagger.ApiDeclaration, error) { if version == "" { version = latest.GroupOrDie("").Version } vers, err := c.ServerAPIVersions() if err != nil { return nil, err } // This check also takes care the case that kubectl is newer than the running endpoint if stringDoesntExistIn(version, vers.Versions) { return nil, fmt.Errorf("API version: %s is not supported by the server. Use one of: %v", version, vers.Versions) } body, err := c.Get().AbsPath("/swaggerapi/api/" + version).Do().Raw() if err != nil { return nil, err } var schema swagger.ApiDeclaration err = json.Unmarshal(body, &schema) if err != nil { return nil, fmt.Errorf("got '%s': %v", string(body), err) } return &schema, nil }
// SetKubernetesDefaults sets default values on the provided client config for accessing the // Kubernetes API or returns an error if any of the defaults are impossible or invalid. // TODO: this method needs to be split into one that sets defaults per group, expected to be fix in PR "Refactoring clientcache.go and helper.go #14592" func SetKubernetesDefaults(config *Config) error { if config.Prefix == "" { config.Prefix = "/api" } if len(config.UserAgent) == 0 { config.UserAgent = DefaultKubernetesUserAgent() } if len(config.Version) == 0 { config.Version = defaultVersionFor(config) } version := config.Version versionInterfaces, err := latest.GroupOrDie("").InterfacesFor(version) if err != nil { return fmt.Errorf("API version '%s' is not recognized (valid values: %s)", version, strings.Join(latest.GroupOrDie("").Versions, ", ")) } if config.Codec == nil { config.Codec = versionInterfaces.Codec } if config.QPS == 0.0 { config.QPS = 5.0 } if config.Burst == 0 { config.Burst = 10 } return nil }
// defaultVersionFor is shared between defaultServerUrlFor and RESTClientFor func defaultVersionFor(config *Config) string { version := config.Version if version == "" { // Clients default to the preferred code API version // TODO: implement version negotiation (highest version supported by server) version = latest.GroupOrDie("").Version } return version }
// SavePodToFile will encode and save a pod to a given path & permissions func SavePodToFile(pod *api.Pod, filePath string, perm os.FileMode) error { if filePath == "" { return fmt.Errorf("file path not specified") } data, err := latest.GroupOrDie("").Codec.Encode(pod) if err != nil { return fmt.Errorf("failed encoding pod: %v", err) } return ioutil.WriteFile(filePath, data, perm) }
// Delete deletes a job, returns error if one occurs. func (c *jobs) Delete(name string, options *api.DeleteOptions) (err error) { if options == nil { return c.r.Delete().Namespace(c.ns).Resource("jobs").Name(name).Do().Error() } body, err := api.Scheme.EncodeToVersion(options, latest.GroupOrDie("").GroupVersion) if err != nil { return err } return c.r.Delete().Namespace(c.ns).Resource("jobs").Name(name).Body(body).Do().Error() }
// LoadPodFromFile will read, decode, and return a Pod from a file. func LoadPodFromFile(filePath string) (*api.Pod, error) { if filePath == "" { return nil, fmt.Errorf("file path not specified") } podDef, err := ioutil.ReadFile(filePath) if err != nil { return nil, fmt.Errorf("failed to read file path %s: %+v", filePath, err) } if len(podDef) == 0 { return nil, fmt.Errorf("file was empty: %s", filePath) } pod := &api.Pod{} if err := latest.GroupOrDie("").Codec.DecodeInto(podDef, pod); err != nil { return nil, fmt.Errorf("failed decoding file: %v", err) } return pod, nil }