Skip to content

Commit

Permalink
Output service url after deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaLanziani committed Jul 13, 2023
1 parent 48a0565 commit dda46e8
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/services/k8s/knative.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ func Apply(namespace string, config *rest.Config, project *project.Project, dock
logger.PrintInfo("Deploying Knative service to " + config.Host)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
defer cancel()
fmt.Println(dockerImage.RemoteTag())
service, err := loadManifest(project, dockerImage)

serviceManifest, err := loadManifest(project, dockerImage)
if err != nil {
return err
}
Expand All @@ -88,8 +88,8 @@ func Apply(namespace string, config *rest.Config, project *project.Project, dock
return fmt.Errorf("Error creating the knative client %v", err)
}

service.ObjectMeta.Namespace = namespace
service.ObjectMeta.Name = project.Name
serviceManifest.ObjectMeta.Namespace = namespace
serviceManifest.ObjectMeta.Name = project.Name

client, err := kubernetes.NewForConfig(config)
if err != nil {
Expand All @@ -98,29 +98,43 @@ func Apply(namespace string, config *rest.Config, project *project.Project, dock

_, err = client.CoreV1().Namespaces().Create(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: service.ObjectMeta.Namespace,
Name: serviceManifest.ObjectMeta.Namespace,
},
}, metav1.CreateOptions{})

if err != nil && !apimachineryErrors.IsAlreadyExists(err) {
return fmt.Errorf("cannot create namespace %s, failed with %v", service.ObjectMeta.Namespace, err)
return fmt.Errorf("cannot create namespace %s, failed with %v", serviceManifest.ObjectMeta.Namespace, err)
}

getService, err := servingClient.Services(service.ObjectMeta.Namespace).Get(ctx, service.ObjectMeta.Name, metav1.GetOptions{})
service, err := servingClient.Services(serviceManifest.ObjectMeta.Namespace).Get(ctx, serviceManifest.ObjectMeta.Name, metav1.GetOptions{})
var deployedService *servingv1.Service
if err != nil {
deployedService, err = servingClient.Services(service.ObjectMeta.Namespace).Create(ctx, service, metav1.CreateOptions{})
deployedService, err = servingClient.Services(serviceManifest.ObjectMeta.Namespace).Create(ctx, serviceManifest, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("Creating Knative service %v", err)
}
} else {
deployedService, err = servingClient.Services(service.ObjectMeta.Namespace).Update(ctx, getService, metav1.UpdateOptions{})
deployedService, err = servingClient.Services(serviceManifest.ObjectMeta.Namespace).Update(ctx, service, metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("Updating Knative service %v", err)
}
}

fmt.Printf("Knative service %q deployed successfully in namespace %s.\n", deployedService.GetObjectMeta().GetName(), service.ObjectMeta.Namespace)
fmt.Printf("Knative service %q deployed successfully in namespace %s.\n", deployedService.GetObjectMeta().GetName(), serviceManifest.ObjectMeta.Namespace)

for {
service, err = servingClient.Services(serviceManifest.ObjectMeta.Namespace).Get(ctx, serviceManifest.ObjectMeta.Name, metav1.GetOptions{})
if err != nil {
return err
}
if service.Status.URL != nil {
fmt.Printf("You can reach it via %s\n", service.Status.URL)
break
}

time.Sleep(time.Millisecond * 500)
}

return nil
}

Expand Down

0 comments on commit dda46e8

Please sign in to comment.