Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add-SupervisorService does not allow for services that do not need data yaml for registration #763

Open
1 of 4 tasks
benhtodd opened this issue Sep 20, 2024 · 3 comments · May be fixed by #785
Open
1 of 4 tasks
Assignees
Labels
bug Bug vvs/dri Developer Ready Infrastructure
Milestone

Comments

@benhtodd
Copy link

Code of Conduct

  • I have read and agree to the Code of Conduct.
  • Vote on this issue by adding a 👍 reaction to the original issue initial description to help the maintainers prioritize.
  • Do not leave "+1" or other comments that do not add relevant information or questions.
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment.

VMware Cloud Foundation

5.2

Module Version

2.11.1.1010

PowerShell Version

7.4.4

PowerCLI Version

13.2.1

PowerVCF Version

2.4.1.1000

Guest Operating System

Windows 11

Environment Details

No response

Description

Using Add-SupervisorService to register CCI does not seem to work. I have tried without the parameter -configureYaml .as well as pass it an empty yml file. Neither works. The first does not seem to return, does not register the servicee at all, I have to break. And the second returns the error

Registration of Supervisor Service (cci-service.fling.vsphere.vmware.com): SUCCESFUL
Error at Script Line 12877
Relevant Command: $supervisorServiceConfigurationYamlArray = [System.Text.Encoding]::UTF8.GetBytes($supervisorServiceConfigurationYaml)
Error Message: Exception calling "GetBytes" with "1" argument(s): "Value cannot be null. (Parameter 'chars')"

And at least registers or adds the service to WCP, but I then need to manually install it to the supervisor

Error or Debug Output

I have none

Expected Behavior

To register and install the service to he supervisor

Actual Behavior

In description

Steps to Reproduce

Add-SupervisorService -Server $sddcManagerFqdn -User $sddcManagerUser -Pass $sddcManagerPass -sddcDomain $sddcDomainName -Cluster $wmClusterName -registerYaml ..\cci-supervisor-service.yml -configureYaml ..\cci-supervisor-service-empty.yml

Log Fragments and Files

No response

Screenshots

No response

References

No response

@benhtodd benhtodd added bug Bug needs-triage Needs Triage labels Sep 20, 2024
@github-actions github-actions bot added the pending-review Pending Review label Sep 20, 2024
@benhtodd
Copy link
Author

I worked around this issue by adding

cci:
configFileContents: {}

to the top of the configYAML so it was not empty

@tenthirtyam tenthirtyam removed pending-review Pending Review needs-triage Needs Triage labels Sep 20, 2024
@tenthirtyam tenthirtyam added this to the Backlog milestone Sep 20, 2024
@tenthirtyam tenthirtyam added the vvs/dri Developer Ready Infrastructure label Sep 20, 2024
@tenthirtyam tenthirtyam changed the title Add-SupervisorService Does not allow for Services that do not need Data Yamls for regiastration Add-SupervisorService does not allow for services that do not need data yaml for registration Sep 23, 2024
@tenthirtyam
Copy link
Collaborator

tenthirtyam commented Nov 15, 2024

Based on the way the function is authored, even though both -registerYaml and -configureYaml are marked as optional, the function actually requires both to work. And, if one is empty, then the GetBytes method throws an exception.

It could be modified as follows:

Function Add-SupervisorService {
    Param (
        [Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String]$server,
        [Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String]$user,
        [Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String]$pass,
        [Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String]$sddcDomain,
        [Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String]$cluster,
        [Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String]$registerYaml,
        [Parameter (Mandatory = $false)] [ValidateNotNullOrEmpty()] [String]$configureYaml
    )

    Try {
        if (Test-VCFConnection -server $server) {
            if (Test-VCFAuthentication -server $server -user $user -pass $pass) {
                if (($vcfVcenterDetails = Get-vCenterServerDetail -server $server -user $user -pass $pass -domain $sddcDomain)) {
                    if (Test-VsphereConnection -server $($vcfVcenterDetails.fqdn)) {
                        if (Test-VsphereAuthentication -server $vcfVcenterDetails.fqdn -user $vcfVcenterDetails.ssoAdmin -pass $vcfVcenterDetails.ssoAdminPass) {
                            if (($clusterDetails = Invoke-ListNamespaceManagementClusters | Where-Object { $_.cluster_name -eq $cluster })) {
                                if ($PsBoundParameters.ContainsKey("registerYaml")) {
                                    if (Test-Path -Path $registerYaml) {
                                        $supervisorService = (($supervisorService = (Select-String -Path $registerYaml -Pattern 'refName: ')) -Split ('refName: '))[-1]
                                        $supervisorServiceVersion = (($supervisorServiceVersion = (Select-String -Path $registerYaml -Pattern 'version: ' -CaseSensitive)) -Split ('version: '))[-1]
                                        $supervisorServiceDisplayName = (($supervisorServiceDisplayName = (Select-String -Path $registerYaml -Pattern 'displayName: ' -CaseSensitive)) -Split ('displayName: '))[-1]
                                        if (-Not (Invoke-ListNamespaceManagementSupervisorServices -Server $DefaultVIServer | Where-Object { $_.display_name -eq $supervisorServiceDisplayName })) {
                                            $supervisorServiceVersionYaml = Get-Content -Path $registerYaml -Raw
                                            $supervisorServiceVersionYamlArray = [System.Text.Encoding]::UTF8.GetBytes($supervisorServiceVersionYaml)
                                            $supervisorServiceVersionYamlbase64 = [System.Convert]::ToBase64String($supervisorServiceVersionYamlArray)
                                            $supervisorServiceCarvelVersionSpec = Initialize-NamespaceManagementSupervisorServicesVersionsCarvelCreateSpec -Content $supervisorServiceVersionYamlbase64
                                            $supervisorServiceCarvelCreateSpec = Initialize-NamespaceManagementSupervisorServicesCarvelCreateSpec -VersionSpec $supervisorServiceCarvelVersionSpec
                                            $supervisorServiceCreateSpec = Initialize-NamespaceManagementSupervisorServicesCreateSpec -CarvelSpec $supervisorServiceCarvelCreateSpec
                                            Invoke-CreateNamespaceManagementSupervisorServices -Server $DefaultVIServer -NamespaceManagementSupervisorServicesCreateSpec $supervisorServiceCreateSpec | Out-Null
                                            if (Invoke-ListNamespaceManagementSupervisorServices -Server $DefaultVIServer | Where-Object { $_.display_name -eq $supervisorServiceDisplayName }) {
                                                Write-Output "Registration of Supervisor Service ($supervisorService): SUCCESSFUL"
                                            } else {
                                                Write-Error "Registration of Supervisor Service ($supervisorService): POST_VALIDATION_FAILED"
                                            }
                                        } else {
                                            Write-Warning "Registration of Supervisor Service ($supervisorService), already exists: SKIPPED"
                                        }
                                    } else {
                                        Write-Error "Supervisor Service Registration YAML (.yml) File ($registerYaml) File Not Found: PRE_VALIDATION_FAILED"
                                    }
                                }
                                if ($PsBoundParameters.ContainsKey("configureYaml")) {
                                    if (Test-Path -Path $configureYaml) {
                                        $supervisorServiceConfigurationYaml = Get-Content -Path $configureYaml -Raw
                                        if (-Not $supervisorServiceConfigurationYaml) {
                                            $supervisorServiceConfigurationYaml = "cci:`n  configFileContents: {}"
                                        }
                                        $supervisorServiceConfigurationYamlArray = [System.Text.Encoding]::UTF8.GetBytes($supervisorServiceConfigurationYaml)
                                        $supervisorServiceConfigurationYamlBase64 = [System.Convert]::ToBase64String($supervisorServiceConfigurationYamlArray)
                                        $supervisorServicesCreateSpec = Initialize-NamespaceManagementSupervisorServicesClusterSupervisorServicesCreateSpec -SupervisorService $supervisorService -Version $supervisorServiceVersion -YamlServiceConfig $supervisorServiceConfigurationYamlBase64
                                        Invoke-CreateClusterNamespaceManagementSupervisorServices -Server $DefaultVIServer -Cluster $clusterDetails.cluster -NamespaceManagementSupervisorServicesClusterSupervisorServicesCreateSpec $supervisorServicesCreateSpec | Out-Null
                                        Start-Sleep 5
                                        if (Invoke-ListClusterNamespaceManagementSupervisorServices -Server $DefaultVIServer -Cluster $clusterDetails.cluster | Where-Object { $_.supervisor_service -eq $supervisorService }) {
                                            Write-Output "Deployment of Supervisor Service ($supervisorService): SUCCESSFUL"
                                        } else {
                                            Write-Error "Deployment of Supervisor Service ($supervisorService): POST_VALIDATION_FAILED"
                                        }
                                    } else {
                                        Write-Error "Supervisor Service Configuration YAML (.yml) File ($configureYaml) File Not Found: PRE_VALIDATION_FAILED"
                                    }
                                } else {
                                    Write-Warning "No configuration YAML provided, skipping deployment."
                                }
                            } else {
                                Write-Error "Unable to locate a Kubernetes Cluster on Cluster ($cluster): PRE_VALIDATION_FAILED"
                            }
                        }
                        Disconnect-VIServer -Server $vcfVcenterDetails.fqdn -Confirm:$false -WarningAction SilentlyContinue | Out-Null
                    }
                }
            }
        }
    } Catch {
        Debug-ExceptionWriter -object $_
    }
}

@tenthirtyam tenthirtyam self-assigned this Nov 15, 2024
@tenthirtyam tenthirtyam modified the milestones: Backlog, v2.13.0 Nov 15, 2024
@tenthirtyam
Copy link
Collaborator

tenthirtyam commented Nov 15, 2024

Alternatively, with guard clauses to reduce the nesting, fail fast, and make it more readable.

Function Add-SupervisorService {
    Param (
        [Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String]$server,
        [Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String]$user,
        [Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String]$pass,
        [Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String]$sddcDomain,
        [Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String]$cluster,
        [Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String]$registerYaml,
        [Parameter (Mandatory = $false)] [ValidateNotNullOrEmpty()] [String]$configureYaml
    )

    Try {
        if (-Not (Test-VCFConnection -server $server)) {
            Write-Error "Failed to connect to VCF server: $server"
            return
        }

        if (-Not (Test-VCFAuthentication -server $server -user $user -pass $pass)) {
            Write-Error "Failed to authenticate to VCF server: $server"
            return
        }

        $vcfVcenterDetails = Get-vCenterServerDetail -server $server -user $user -pass $pass -domain $sddcDomain
        if (-Not $vcfVcenterDetails) {
            Write-Error "Failed to get vCenter server details for domain: $sddcDomain"
            return
        }

        if (-Not (Test-VsphereConnection -server $vcfVcenterDetails.fqdn)) {
            Write-Error "Failed to connect to vSphere server: $($vcfVcenterDetails.fqdn)"
            return
        }

        if (-Not (Test-VsphereAuthentication -server $vcfVcenterDetails.fqdn -user $vcfVcenterDetails.ssoAdmin -pass $vcfVcenterDetails.ssoAdminPass)) {
            Write-Error "Failed to authenticate to vSphere server: $($vcfVcenterDetails.fqdn)"
            return
        }

        $clusterDetails = Invoke-ListNamespaceManagementClusters | Where-Object { $_.cluster_name -eq $cluster }
        if (-Not $clusterDetails) {
            Write-Error "Unable to locate a Kubernetes Cluster on Cluster ($cluster): PRE_VALIDATION_FAILED"
            return
        }

        if (-Not (Test-Path -Path $registerYaml)) {
            Write-Error "Supervisor Service Registration YAML (.yml) File ($registerYaml) File Not Found: PRE_VALIDATION_FAILED"
            return
        }

        $supervisorService = (Select-String -Path $registerYaml -Pattern 'refName: ') -Split 'refName: ' | Select-Object -Last 1
        $supervisorServiceVersion = (Select-String -Path $registerYaml -Pattern 'version: ' -CaseSensitive) -Split 'version: ' | Select-Object -Last 1
        $supervisorServiceDisplayName = (Select-String -Path $registerYaml -Pattern 'displayName: ' -CaseSensitive) -Split 'displayName: ' | Select-Object -Last 1

        if (Invoke-ListNamespaceManagementSupervisorServices -Server $DefaultVIServer | Where-Object { $_.display_name -eq $supervisorServiceDisplayName }) {
            Write-Warning "Registration of Supervisor Service ($supervisorService), already exists: SKIPPED"
        } else {
            $supervisorServiceVersionYaml = Get-Content -Path $registerYaml -Raw
            $supervisorServiceVersionYamlArray = [System.Text.Encoding]::UTF8.GetBytes($supervisorServiceVersionYaml)
            $supervisorServiceVersionYamlbase64 = [System.Convert]::ToBase64String($supervisorServiceVersionYamlArray)
            $supervisorServiceCarvelVersionSpec = Initialize-NamespaceManagementSupervisorServicesVersionsCarvelCreateSpec -Content $supervisorServiceVersionYamlbase64
            $supervisorServiceCarvelCreateSpec = Initialize-NamespaceManagementSupervisorServicesCarvelCreateSpec -VersionSpec $supervisorServiceCarvelVersionSpec
            $supervisorServiceCreateSpec = Initialize-NamespaceManagementSupervisorServicesCreateSpec -CarvelSpec $supervisorServiceCarvelCreateSpec
            Invoke-CreateNamespaceManagementSupervisorServices -Server $DefaultVIServer -NamespaceManagementSupervisorServicesCreateSpec $supervisorServiceCreateSpec | Out-Null
            if (Invoke-ListNamespaceManagementSupervisorServices -Server $DefaultVIServer | Where-Object { $_.display_name -eq $supervisorServiceDisplayName }) {
                Write-Output "Registration of Supervisor Service ($supervisorService): SUCCESSFUL"
            } else {
                Write-Error "Registration of Supervisor Service ($supervisorService): POST_VALIDATION_FAILED"
                return
            }
        }

        if ($PsBoundParameters.ContainsKey("configureYaml")) {
            if (-Not (Test-Path -Path $configureYaml)) {
                Write-Error "Supervisor Service Configuration YAML (.yml) File ($configureYaml) File Not Found: PRE_VALIDATION_FAILED"
                return
            }

            $supervisorServiceConfigurationYaml = Get-Content -Path $configureYaml -Raw
            if (-Not $supervisorServiceConfigurationYaml) {
                $supervisorServiceConfigurationYaml = "cci:`n  configFileContents: {}"
            }
            $supervisorServiceConfigurationYamlArray = [System.Text.Encoding]::UTF8.GetBytes($supervisorServiceConfigurationYaml)
            $supervisorServiceConfigurationYamlBase64 = [System.Convert]::ToBase64String($supervisorServiceConfigurationYamlArray)
            $supervisorServicesCreateSpec = Initialize-NamespaceManagementSupervisorServicesClusterSupervisorServicesCreateSpec -SupervisorService $supervisorService -Version $supervisorServiceVersion -YamlServiceConfig $supervisorServiceConfigurationYamlBase64
            Invoke-CreateClusterNamespaceManagementSupervisorServices -Server $DefaultVIServer -Cluster $clusterDetails.cluster -NamespaceManagementSupervisorServicesClusterSupervisorServicesCreateSpec $supervisorServicesCreateSpec | Out-Null
            Start-Sleep 5
            if (Invoke-ListClusterNamespaceManagementSupervisorServices -Server $DefaultVIServer -Cluster $clusterDetails.cluster | Where-Object { $_.supervisor_service -eq $supervisorService }) {
                Write-Output "Deployment of Supervisor Service ($supervisorService): SUCCESSFUL"
            } else {
                Write-Error "Deployment of Supervisor Service ($supervisorService): POST_VALIDATION_FAILED"
            }
        } else {
            Write-Warning "No configuration YAML provided, skipping deployment."
        }

        Disconnect-VIServer -Server $vcfVcenterDetails.fqdn -Confirm:$false -WarningAction SilentlyContinue | Out-Null
    } Catch {
        Debug-ExceptionWriter -object $_
    }
}

@tenthirtyam tenthirtyam modified the milestones: v2.12.1, Backlog Dec 17, 2024
@tenthirtyam tenthirtyam linked a pull request Dec 17, 2024 that will close this issue
11 tasks
@tenthirtyam tenthirtyam modified the milestones: Backlog, v2.12.1 Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug vvs/dri Developer Ready Infrastructure
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants