github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/examples/gangway/main.go (about) 1 /* 2 Copyright 2023 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "context" 21 "flag" 22 "log" 23 "time" 24 25 "google.golang.org/protobuf/encoding/prototext" 26 pb "sigs.k8s.io/prow/pkg/gangway" 27 gangwayGoogleClient "sigs.k8s.io/prow/pkg/gangway/client/google" 28 ) 29 30 var ( 31 addr = flag.String("addr", "127.0.0.1:50051", "Address of grpc server.") 32 apiKey = flag.String("api-key", "", "API key.") 33 audience = flag.String("audience", "", "Audience.") 34 keyFile = flag.String("key-file", "", "Path to a Google service account key file.") 35 clientPem = flag.String("client-pem", "", "Path to a client.pem file.") 36 ) 37 38 // Use like this: 39 // 40 // go run main.go --key-file=key.json \ 41 // --audience=SERVICE_NAME.endpoints.PROJECT_NAME.cloud.goog \ 42 // --addr=12.34.56.78:443 --api-key=API_KEY --client-pem=client.pem \ 43 // <CreateJobExecutionRequest> 44 // 45 // where <CreateJobExecutionRequest> is the protobuf (in textpb format) message 46 // you want to send over. For example, if you want to run the periodic job named 47 // "foo", use: 48 // 49 // 'job_name: "foo", job_execution_type: 1' 50 // 51 // as the <CreateJobExecutionRequest>. The "1" here for "job_execution_type" 52 // denotes the periodic job type (because this field is an enum, not a string). 53 54 func main() { 55 flag.Parse() 56 57 jobName := "some-job" 58 jobExecutionType := pb.JobExecutionType_PERIODIC 59 60 // Set default values. 61 cjer := pb.CreateJobExecutionRequest{ 62 JobName: jobName, 63 JobExecutionType: jobExecutionType, 64 } 65 66 // Read in string version of a CreateJobExecutionRequest. 67 if len(flag.Args()) > 0 { 68 textpb := flag.Arg(0) 69 if err := prototext.Unmarshal([]byte(textpb), &cjer); err != nil { 70 log.Fatalf("could not unmarshal textpb %q: %v", textpb, err) 71 } 72 } 73 74 log.Printf("creating job execution with %+v", &cjer) 75 76 // Create a Prow API gRPC client that's able to authenticate to Gangway (the 77 // Prow API Server). 78 prowClient, err := gangwayGoogleClient.NewFromFile(*addr, *keyFile, *audience, *clientPem, *apiKey) 79 if err != nil { 80 log.Fatalf("Prow API client creation failed: %v", err) 81 } 82 83 defer prowClient.Close() 84 85 // Create a Context that has credentials injected inside it. 86 ctx, err := prowClient.EmbedCredentials(context.Background()) 87 if err != nil { 88 log.Fatalf("could not create a context with embedded credentials: %v", err) 89 } 90 91 // Trigger job! Because this is gRPC it's just a function call. 92 jobExecution, err := prowClient.GRPC.CreateJobExecution(ctx, &cjer) 93 if err != nil { 94 log.Fatalf("could not trigger job: %v", err) 95 } 96 97 log.Printf("triggered job: %v", jobExecution) 98 99 // Poll to see if our job has succeeded. 100 pollInterval := 2 * time.Second 101 timeout := 90 * time.Second 102 expectedStatus := pb.JobExecutionStatus_SUCCESS 103 104 if err := prowClient.WaitForJobExecutionStatus(ctx, jobExecution.Id, pollInterval, timeout, expectedStatus); err != nil { 105 log.Fatalf("failed: %v", err) 106 } 107 108 log.Printf("job succeeded!") 109 }