Skip to content

Commit

Permalink
still draft
Browse files Browse the repository at this point in the history
Signed-off-by: lakshmimsft <[email protected]>
  • Loading branch information
lakshmimsft committed Dec 17, 2024
1 parent 1e20af2 commit 423706a
Show file tree
Hide file tree
Showing 17 changed files with 545 additions and 674 deletions.
27 changes: 0 additions & 27 deletions cmd/ucpd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,33 +66,6 @@ var rootCmd = &cobra.Command{
if err != nil {
return err
}
/*
// Discuss if there is a better way to check if the server is listening..
// Start RegisterManifests in a goroutine after 15 seconds
go func() {
time.Sleep(15 * time.Second)
// Register manifests
manifestDir := options.Config.Manifests.ManifestDirectory
if _, err := os.Stat(manifestDir); os.IsNotExist(err) {
logger.Error(err, "Manifest directory does not exist", "directory", manifestDir)
return
} else if err != nil {
logger.Error(err, "Error checking manifest directory", "directory", manifestDir)
return
}
ucpclient, err := ucpclient.NewUCPClient(options.UCPConnection)
if err != nil {
logger.Error(err, "Failed to create UCP client")
}
if err := ucpclient.RegisterManifests(cmd.Context(), manifestDir); err != nil {
logger.Error(err, "Failed to register manifests")
} else {
logger.Info("Successfully registered manifests", "directory", manifestDir)
}
}()
*/

ctx := logr.NewContext(cmd.Context(), logger)
return hosting.RunWithInterrupts(ctx, host)
Expand Down
61 changes: 9 additions & 52 deletions pkg/cli/cmd/resourceprovider/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@ package create
import (
"context"

v1 "github.com/radius-project/radius/pkg/armrpc/api/v1"
"github.com/radius-project/radius/pkg/cli"
"github.com/radius-project/radius/pkg/cli/cmd"
"github.com/radius-project/radius/pkg/cli/cmd/commonflags"
"github.com/radius-project/radius/pkg/cli/cmd/resourceprovider/common"
"github.com/radius-project/radius/pkg/cli/connections"
"github.com/radius-project/radius/pkg/cli/framework"
"github.com/radius-project/radius/pkg/cli/manifest"
"github.com/radius-project/radius/pkg/cli/output"
"github.com/radius-project/radius/pkg/cli/workspaces"
"github.com/radius-project/radius/pkg/to"
"github.com/radius-project/radius/pkg/ucp/api/v20231001preview"
"github.com/radius-project/radius/pkg/ucp/ucpclient"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -114,65 +113,23 @@ func (r *Runner) Validate(cmd *cobra.Command, args []string) error {

// Run runs the `rad resource-provider create` command.
func (r *Runner) Run(ctx context.Context) error {
client, err := r.ConnectionFactory.CreateApplicationsManagementClient(ctx, *r.Workspace)

connection, err := cmd.GetConnection(ctx, r.Workspace)
if err != nil {
return err
}

r.Output.LogInfo("Creating resource provider %s", r.ResourceProvider.Name)
_, err = client.CreateOrUpdateResourceProvider(ctx, "local", r.ResourceProvider.Name, &v20231001preview.ResourceProviderResource{
Location: to.Ptr(v1.LocationGlobal),
Properties: &v20231001preview.ResourceProviderProperties{},
})
ucpclient, err := ucpclient.NewUCPClient(connection)
if err != nil {
return err
}

// The location resource contains references to all of the resource types and API versions that the resource provider supports.
// We're instantiating the struct here so we can update it as we loop.
locationResource := v20231001preview.LocationResource{
Properties: &v20231001preview.LocationProperties{
ResourceTypes: map[string]*v20231001preview.LocationResourceType{},
},
}

for resourceTypeName, resourceType := range r.ResourceProvider.Types {
r.Output.LogInfo("Creating resource type %s/%s", r.ResourceProvider.Name, resourceTypeName)
_, err := client.CreateOrUpdateResourceType(ctx, "local", r.ResourceProvider.Name, resourceTypeName, &v20231001preview.ResourceTypeResource{
Properties: &v20231001preview.ResourceTypeProperties{
DefaultAPIVersion: resourceType.DefaultAPIVersion,
},
})
if err != nil {
return err
}

locationResourceType := &v20231001preview.LocationResourceType{
APIVersions: map[string]map[string]any{},
}

for apiVersionName := range resourceType.APIVersions {
r.Output.LogInfo("Creating API Version %s/%s@%s", r.ResourceProvider.Name, resourceTypeName, apiVersionName)
_, err := client.CreateOrUpdateAPIVersion(ctx, "local", r.ResourceProvider.Name, resourceTypeName, apiVersionName, &v20231001preview.APIVersionResource{
Properties: &v20231001preview.APIVersionProperties{},
})
if err != nil {
return err
}

locationResourceType.APIVersions[apiVersionName] = map[string]any{}
}

locationResource.Properties.ResourceTypes[resourceTypeName] = locationResourceType
}

r.Output.LogInfo("Creating location %s/%s", r.ResourceProvider.Name, v1.LocationGlobal)
_, err = client.CreateOrUpdateLocation(ctx, "local", r.ResourceProvider.Name, v1.LocationGlobal, &locationResource)
if err != nil {
return err
// Proceed with registering manifests
if err := ucpclient.RegisterManifests(ctx, r.ResourceProviderManifestFilePath); err != nil {
return nil
}

response, err := client.GetResourceProvider(ctx, "local", r.ResourceProvider.Name)
response, err := ucpclient.GetResourceProvider(ctx, "local", r.ResourceProvider.Name)
if err != nil {
return err
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/cli/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ package cmd

import (
"context"
"errors"
"fmt"

"github.com/radius-project/radius/pkg/cli/aws"
"github.com/radius-project/radius/pkg/cli/azure"
"github.com/radius-project/radius/pkg/cli/clients"
"github.com/radius-project/radius/pkg/cli/clierrors"
"github.com/radius-project/radius/pkg/cli/workspaces"
corerp "github.com/radius-project/radius/pkg/corerp/api/v20231001preview"
"github.com/radius-project/radius/pkg/sdk"
"github.com/radius-project/radius/pkg/to"
)

Expand Down Expand Up @@ -94,3 +97,20 @@ func CheckIfRecipeExists(ctx context.Context, client clients.ApplicationsManagem

return envResource, recipeProperties, nil
}

// GetConnection from Workspace.
func GetConnection(ctx context.Context, workspace *workspaces.Workspace) (sdk.Connection, error) {
connection, err := workspace.Connect()
if err != nil {
return nil, err
}

err = sdk.TestConnection(ctx, connection)
if errors.Is(err, &sdk.ErrRadiusNotInstalled{}) {
return nil, clierrors.MessageWithCause(err, "Could not connect to Radius.")
} else if err != nil {
return nil, err
}

return connection, nil
}
151 changes: 151 additions & 0 deletions pkg/cli/manifest/registermanifest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package manifest

import (
"context"
"fmt"
"os"
"path/filepath"

v1 "github.com/radius-project/radius/pkg/armrpc/api/v1"
"github.com/radius-project/radius/pkg/to"
"github.com/radius-project/radius/pkg/ucp/api/v20231001preview"
)

// RegisterFile registers a manifest file
func RegisterFile(ctx context.Context, clientFactory v20231001preview.ClientFactory, planeName string, filePath string, logger func(string)) error {
// Check for valid file path
if filePath == "" {
return fmt.Errorf("invalid manifest file path")
}

// Read the manifest file
resourceProvider, err := ReadFile(filePath)
if err != nil {
return err
}

resourceProviderPoller, err := clientFactory.NewResourceProvidersClient().BeginCreateOrUpdate(ctx, planeName, resourceProvider.Name, v20231001preview.ResourceProviderResource{
Location: to.Ptr(v1.LocationGlobal),
Properties: &v20231001preview.ResourceProviderProperties{},
}, nil)
if err != nil {
return err
}

_, err = resourceProviderPoller.PollUntilDone(ctx, nil)
if err != nil {
return err
}

// The location resource contains references to all of the resource types and API versions that the resource provider supports.
// We're instantiating the struct here so we can update it as we loop.
locationResource := v20231001preview.LocationResource{
Properties: &v20231001preview.LocationProperties{
ResourceTypes: map[string]*v20231001preview.LocationResourceType{},
},
}

for resourceTypeName, resourceType := range resourceProvider.Types {
resourceTypePoller, err := clientFactory.NewResourceTypesClient().BeginCreateOrUpdate(ctx, planeName, resourceProvider.Name, resourceTypeName, v20231001preview.ResourceTypeResource{
Properties: &v20231001preview.ResourceTypeProperties{
DefaultAPIVersion: resourceType.DefaultAPIVersion,
},
}, nil)
if err != nil {
return err
}

_, err = resourceTypePoller.PollUntilDone(ctx, nil)
if err != nil {
return err
}

locationResourceType := &v20231001preview.LocationResourceType{
APIVersions: map[string]map[string]any{},
}

for apiVersionName := range resourceType.APIVersions {
apiVersionsPoller, err := clientFactory.NewAPIVersionsClient().BeginCreateOrUpdate(ctx, planeName, resourceProvider.Name, resourceTypeName, apiVersionName, v20231001preview.APIVersionResource{
Properties: &v20231001preview.APIVersionProperties{},
}, nil)
if err != nil {
return err
}

_, err = apiVersionsPoller.PollUntilDone(ctx, nil)
if err != nil {
return err
}

locationResourceType.APIVersions[apiVersionName] = map[string]any{}
}

locationResource.Properties.ResourceTypes[resourceTypeName] = locationResourceType
}

locationPoller, err := clientFactory.NewLocationsClient().BeginCreateOrUpdate(ctx, planeName, resourceProvider.Name, v1.LocationGlobal, locationResource, nil)
if err != nil {
return err
}

_, err = locationPoller.PollUntilDone(ctx, nil)
if err != nil {
return err
}

_, err = clientFactory.NewResourceProvidersClient().Get(ctx, planeName, resourceProvider.Name, nil)
if err != nil {
return err
}

return nil
}

// RegisterDirectory registers all manifest files in a directory
func RegisterDirectory(ctx context.Context, clientFactory v20231001preview.ClientFactory, planeName string, directoryPath string, logger func(string)) error {
// Check for valid directory path
if directoryPath == "" {
return fmt.Errorf("invalid manifest directory")
}

info, err := os.Stat(directoryPath)
if err != nil {
return fmt.Errorf("failed to access manifest path %s: %w", directoryPath, err)
}

if !info.IsDir() {
return fmt.Errorf("manifest path %s is not a directory", directoryPath)
}

// List all files in the manifestDirectory
files, err := os.ReadDir(directoryPath)
if err != nil {
return err
}

// Iterate over each file in the directory
for _, fileInfo := range files {
if fileInfo.IsDir() {
continue // Skip directories - TBD: check if want to include subdirectories
}
filePath := filepath.Join(directoryPath, fileInfo.Name())

err = RegisterFile(ctx, clientFactory, planeName, filePath, logger)
if err != nil {
return fmt.Errorf("failed to register manifest file %s: %w", filePath, err)
}
}

return nil
}
Loading

0 comments on commit 423706a

Please sign in to comment.