@@ -20,10 +20,13 @@ import (
2020 "github.com/project-ai-services/ai-services/internal/pkg/cli/templates"
2121 "github.com/project-ai-services/ai-services/internal/pkg/constants"
2222 "github.com/project-ai-services/ai-services/internal/pkg/logger"
23+ "github.com/project-ai-services/ai-services/internal/pkg/models"
2324 "github.com/project-ai-services/ai-services/internal/pkg/runtime"
2425 "github.com/project-ai-services/ai-services/internal/pkg/runtime/podman"
26+ "github.com/project-ai-services/ai-services/internal/pkg/specs"
2527 "github.com/project-ai-services/ai-services/internal/pkg/spinner"
2628 "github.com/project-ai-services/ai-services/internal/pkg/utils"
29+ "github.com/project-ai-services/ai-services/internal/pkg/validators"
2730 "github.com/project-ai-services/ai-services/internal/pkg/vars"
2831)
2932
@@ -105,31 +108,20 @@ var createCmd = &cobra.Command{
105108 }
106109 s .Stop ("SMT level configured successfully" )
107110
108- // Fetch all the application Template names
109- appTemplateNames , err := helpers .FetchApplicationTemplatesNames ()
110- if err != nil {
111- return fmt .Errorf ("failed to list templates: %w" , err )
112- }
113-
114- var appTemplateName string
111+ tp := templates .NewEmbedTemplateProvider (templates.EmbedOptions {})
115112
116- if index := fetchAppTemplateIndex (appTemplateNames , templateName ); index == - 1 {
117- return errors .New ("provided template name is wrong. Please provide a valid template name" )
118- } else {
119- appTemplateName = appTemplateNames [index ]
113+ // validate whether the provided template name is correct
114+ if err := validators .ValidateAppTemplateExist (tp , templateName ); err != nil {
115+ return err
120116 }
121117
122- applicationPodTemplatesPath := applicationPath + appTemplateName + "/templates"
123-
124- tmpls , err := helpers .LoadAllTemplates (applicationPodTemplatesPath )
118+ tmpls , err := tp .LoadAllTemplates (templateName + "/templates" )
125119 if err != nil {
126120 return fmt .Errorf ("failed to parse the templates: %w" , err )
127121 }
128122
129- tp := templates .NewEmbedTemplateProvider (templates.EmbedOptions {})
130-
131123 // load metadata.yml to read the app metadata
132- appMetadata , err := tp .LoadMetadata (appTemplateName )
124+ appMetadata , err := tp .LoadMetadata (templateName )
133125 if err != nil {
134126 return fmt .Errorf ("failed to read the app metadata: %w" , err )
135127 }
@@ -141,7 +133,7 @@ var createCmd = &cobra.Command{
141133 // ---- Validate Spyre card Requirements ----
142134
143135 // calculate the required spyre cards
144- reqSpyreCardsCount , err := calculateReqSpyreCards (utils .ExtractMapKeys (tmpls ), applicationPodTemplatesPath )
136+ reqSpyreCardsCount , err := calculateReqSpyreCards (tp , utils .ExtractMapKeys (tmpls ), templateName )
145137 if err != nil {
146138 return err
147139 }
@@ -165,7 +157,7 @@ var createCmd = &cobra.Command{
165157 if ! skipModelDownload {
166158 s = spinner .New ("Downloading models as part of application creation..." )
167159 s .Start (ctx )
168- models , err := helpers .ListModels (appTemplateName )
160+ models , err := helpers .ListModels (templateName )
169161 if err != nil {
170162 s .Fail ("failed to list models" )
171163 return err
@@ -196,15 +188,15 @@ var createCmd = &cobra.Command{
196188 s = spinner .New ("Deploying application '" + appName + "'..." )
197189 s .Start (ctx )
198190 // execute the pod Templates
199- if err := executePodTemplates (runtime , appName , appMetadata , tmpls , applicationPodTemplatesPath , pciAddresses ); err != nil {
191+ if err := executePodTemplates (runtime , tp , appName , appMetadata , tmpls , pciAddresses ); err != nil {
200192 return err
201193 }
202194 s .Stop ("Application '" + appName + "' deployed successfully" )
203195
204196 logger .Infoln ("-------" )
205197
206198 // print the next steps to be performed at the end of create
207- if err := helpers .PrintNextSteps (runtime , appName , appTemplateName ); err != nil {
199+ if err := helpers .PrintNextSteps (runtime , appName , templateName ); err != nil {
208200 // do not want to fail the overall create if we cannot print next steps
209201 logger .Infof ("failed to display next steps: %v\n " , err )
210202 return nil
@@ -306,44 +298,22 @@ func setSMTLevel() error {
306298}
307299
308300func getTargetSMTLevel () (* int , error ) {
309- appTemplateNames , err := helpers .FetchApplicationTemplatesNames ()
310- if err != nil {
311- return nil , fmt .Errorf ("failed to list templates: %w" , err )
312- }
313-
314- var appTemplateName string
301+ tp := templates .NewEmbedTemplateProvider (templates.EmbedOptions {})
315302
316- if index := fetchAppTemplateIndex (appTemplateNames , templateName ); index == - 1 {
317- return nil , errors .New ("provided template name is wrong. Please provide a valid template name" )
318- } else {
319- appTemplateName = appTemplateNames [index ]
303+ // validate whether the provided template name is correct
304+ if err := validators .ValidateAppTemplateExist (tp , templateName ); err != nil {
305+ return nil , err
320306 }
321307
322- tp := templates .NewEmbedTemplateProvider (templates.EmbedOptions {})
323-
324308 // load metadata.yml to read the app metadata
325- appMetadata , err := tp .LoadMetadata (appTemplateName )
309+ appMetadata , err := tp .LoadMetadata (templateName )
326310 if err != nil {
327311 return nil , fmt .Errorf ("failed to read the app metadata: %w" , err )
328312 }
329313
330314 return appMetadata .SMTLevel , nil
331315}
332316
333- // fetchAppTemplateIndex -> Returns the index of app template if exists, otherwise -1
334- func fetchAppTemplateIndex (appTemplateNames []string , templateName string ) int {
335- appTemplateIndex := - 1
336-
337- for index , appTemplateName := range appTemplateNames {
338- if strings .EqualFold (appTemplateName , templateName ) {
339- appTemplateIndex = index
340- break
341- }
342- }
343-
344- return appTemplateIndex
345- }
346-
347317func verifyPodTemplateExists (tmpls map [string ]* template.Template , appMetadata * templates.AppMetadata ) error {
348318 flattenPodTemplateExecutions := utils .FlattenArray (appMetadata .PodTemplateExecutions )
349319
@@ -361,8 +331,8 @@ func verifyPodTemplateExists(tmpls map[string]*template.Template, appMetadata *t
361331 return nil
362332}
363333
364- func executePodTemplates (runtime runtime.Runtime , appName string , appMetadata * templates.AppMetadata ,
365- tmpls map [string ]* template.Template , podTemplatesPath string , pciAddresses []string ) error {
334+ func executePodTemplates (runtime runtime.Runtime , tp templates. Template , appName string , appMetadata * templates.AppMetadata ,
335+ tmpls map [string ]* template.Template , pciAddresses []string ) error {
366336
367337 globalParams := map [string ]any {
368338 "AppName" : appName ,
@@ -390,10 +360,8 @@ func executePodTemplates(runtime runtime.Runtime, appName string, appMetadata *t
390360 // Shallow Copy globalParams Map
391361 params := utils .CopyMap (globalParams )
392362
393- podTemplateFilePath := podTemplatesPath + "/" + podTemplateName
394-
395363 // fetch pod Spec
396- podSpec , err := fetchPodSpec (podTemplateFilePath )
364+ podSpec , err := fetchPodSpec (tp , templateName , podTemplateName )
397365 if err != nil {
398366 errCh <- err
399367 }
@@ -496,18 +464,15 @@ func validateSpyreCardRequirements(req int, actual int) error {
496464 return nil
497465}
498466
499- func calculateReqSpyreCards (podTemplateFileNames []string , podTemplatesPath string ) (int , error ) {
467+ func calculateReqSpyreCards (tp templates. Template , podTemplateFileNames []string , appTemplateName string ) (int , error ) {
500468 totalReqSpyreCounts := 0
501469
502470 // Calculate Req Spyre Counts
503471 for _ , podTemplateFileName := range podTemplateFileNames {
504-
505- podTemplateFilePath := podTemplatesPath + "/" + podTemplateFileName
506-
507- // load the pod Template
508- podSpec , err := helpers .LoadPodTemplate (podTemplateFilePath )
472+ // fetch pod spec
473+ podSpec , err := fetchPodSpec (tp , appTemplateName , podTemplateFileName )
509474 if err != nil {
510- return totalReqSpyreCounts , fmt .Errorf ("failed to load pod Template: %s with error: %w" , podTemplateFilePath , err )
475+ return totalReqSpyreCounts , fmt .Errorf ("failed to load pod Template: '%s' for appTemplate: '%s' with error: %w" , podTemplateFileName , appTemplateName , err )
511476 }
512477
513478 // fetch the spyreCount for all containers from the annotations
@@ -550,23 +515,23 @@ func fetchSpyreCardsFromPodAnnotations(annotations map[string]string) (int, map[
550515 return spyreCards , spyreCardContainerMap , nil
551516}
552517
553- func fetchPodSpec (podTemplateFilePath string ) (* helpers .PodSpec , error ) {
554- podSpec , err := helpers . LoadPodTemplate ( podTemplateFilePath )
518+ func fetchPodSpec (tp templates. Template , appTemplateName , podTemplateFileName string ) (* models .PodSpec , error ) {
519+ podSpec , err := tp . LoadPodTemplateWithDummyParams ( appTemplateName , podTemplateFileName )
555520 if err != nil {
556- return nil , fmt .Errorf ("failed to load pod Template: %s with error: %w" , podTemplateFilePath , err )
521+ return nil , fmt .Errorf ("failed to load pod Template: '%s' for appTemplate: '%s' with error: %w" , podTemplateFileName , appTemplateName , err )
557522 }
558523
559524 return podSpec , nil
560525}
561526
562- func fetchPodAnnotations (podSpec * helpers .PodSpec ) map [string ]string {
563- return helpers .FetchPodAnnotations (* podSpec )
527+ func fetchPodAnnotations (podSpec * models .PodSpec ) map [string ]string {
528+ return specs .FetchPodAnnotations (* podSpec )
564529}
565530
566- func returnEnvParamsForPod (podSpec * helpers .PodSpec , podAnnotations map [string ]string , pciAddresses * []string ) (map [string ]map [string ]string , error ) {
531+ func returnEnvParamsForPod (podSpec * models .PodSpec , podAnnotations map [string ]string , pciAddresses * []string ) (map [string ]map [string ]string , error ) {
567532
568533 env := map [string ]map [string ]string {}
569- podContainerNames := helpers .FetchContainerNames (* podSpec )
534+ podContainerNames := specs .FetchContainerNames (* podSpec )
570535
571536 // populate env with empty map
572537 for _ , containerName := range podContainerNames {
0 commit comments