func GetSecretFixture(filename string) *kapi.Secret { var secret kapi.Secret jsonData, err := ioutil.ReadFile(filename) if err != nil { fmt.Printf("ERROR: Unable to read %s: %v", filename, err) return nil } latest.CodecForLegacyGroup().DecodeInto(jsonData, &secret) return &secret }
// GetBuildFixture reads the Build JSON and returns and Build object func GetBuildFixture(filename string) *buildapi.Build { var build buildapi.Build jsonData, err := ioutil.ReadFile(filename) if err != nil { fmt.Printf("ERROR: Unable to read %s: %v", filename, err) return nil } latest.CodecForLegacyGroup().DecodeInto(jsonData, &build) return &build }
func (o CreateNodeConfigOptions) MakeNodeJSON(nodeJSONFile string) error { node := &kapi.Node{} node.Name = o.NodeName json, err := klatest.CodecForLegacyGroup().Encode(node) if err != nil { return err } if err := ioutil.WriteFile(nodeJSONFile, json, 0644); err != nil { return err } return nil }
func GetTemplateFixture(filename string) (*templateapi.Template, error) { data, err := ioutil.ReadFile(filename) if err != nil { return nil, err } jsonData, err := kyaml.ToJSON(data) if err != nil { return nil, err } obj, err := latest.CodecForLegacyGroup().Decode(jsonData) if err != nil { return nil, err } return obj.(*templateapi.Template), nil }
// CreateSampleImageStream creates an ImageStream in given namespace func CreateSampleImageStream(namespace string) *imageapi.ImageStream { var stream imageapi.ImageStream jsonData, err := ioutil.ReadFile("fixtures/test-image-stream.json") if err != nil { fmt.Printf("ERROR: Unable to read: %v", err) return nil } latest.CodecForLegacyGroup().DecodeInto(jsonData, &stream) client, _ := GetClusterAdminClient(KubeConfigPath()) result, err := client.ImageStreams(namespace).Create(&stream) if err != nil { fmt.Printf("ERROR: Unable to create sample ImageStream: %v\n", err) return nil } return result }
// forbidden renders a simple forbidden error func forbidden(reason string, attributes authorizer.AuthorizationAttributes, w http.ResponseWriter, req *http.Request) { kind := "" name := "" apiVersion := klatest.DefaultVersionForLegacyGroup() // the attributes can be empty for two basic reasons: // 1. malformed API request // 2. not an API request at all // In these cases, just assume default that will work better than nothing if attributes != nil { apiVersion = attributes.GetAPIVersion() kind = attributes.GetResource() if len(attributes.GetAPIGroup()) > 0 { kind = attributes.GetAPIGroup() + "." + kind } name = attributes.GetResourceName() } // Reason is an opaque string that describes why access is allowed or forbidden (forbidden by the time we reach here). // We don't have direct access to kind or name (not that those apply either in the general case) // We create a NewForbidden to stay close the API, but then we override the message to get a serialization // that makes sense when a human reads it. forbiddenError, _ := kapierrors.NewForbidden(kind, name, errors.New("") /*discarded*/).(*kapierrors.StatusError) forbiddenError.ErrStatus.Message = reason // Not all API versions in valid API requests will have a matching codec in kubernetes. If we can't find one, // just default to the latest kube codec. codec := klatest.CodecForLegacyGroup() if requestedCodec, err := klatest.InterfacesForLegacyGroup(apiVersion); err == nil { codec = requestedCodec } formatted := &bytes.Buffer{} output, err := codec.Encode(&forbiddenError.ErrStatus) if err != nil { fmt.Fprintf(formatted, "%s", forbiddenError.Error()) } else { json.Indent(formatted, output, "", " ") } w.Header().Set("Content-Type", restful.MIME_JSON) w.WriteHeader(http.StatusForbidden) w.Write(formatted.Bytes()) }