def cluster_roles()

in eksupdate/src/preflight_module.py [0:0]


def cluster_roles(preflight,log_details,errors,cluster_name,region,report,customer_report):
    loading_config(cluster_name,region)
    s3 = boto3.resource('s3')
    # Cluster Roles for proper functioning
    # cluster_roles_list = s3.Object('eks-one-click-upgrade', 'cluster_roles.json')
    # cluster_roles_list = cluster_roles_list.get()['Body'].read().decode('utf-8')
    # cluster_roles_list = json.loads(cluster_roles_list)
    f = open('eksupdate/src/S3Files/cluster_roles.json',)
    cluster_roles_list = json.load(f)
    #print(cluster_roles_list)
    if preflight :
        cluster_roles_list = cluster_roles_list['preflight']
    else :
        cluster_roles_list = cluster_roles_list['postflight']
    # print(cluster_roles_list)
    try : 
        print('\n')
        log_pusher(log_details,cluster_name,region,'Checking important cluster role are present or not .....')
        print('Checking important cluster role are present or not .....')
        available = []
        not_available = []
        customer_report['cluster role'] = []
        for role in cluster_roles_list['roles']:
            try : 
                v1=client.RbacAuthorizationV1Api()
                fs = 'metadata.name=' + role
                res=v1.list_cluster_role(field_selector = fs)
                if(len(res.items) > 0):
                    available.append(role)
                    #print(available)
                    #print(res)
                else : 
                    not_available.append(role)
                    #print(not_available)
                    #print('Unable to find ' + role)
            except:
                customer_report['cluster role'].append('Some error occured while checking role for ' + role)
                log_pusher(log_details,cluster_name,region,'Some error occured while checking role for ' + role)
                print('Some error occured while checking role for ' + role)
        #report['cluster-roles'] = {'available' : available,'not-available':not_available}
        if report['cluster']['version'] in cluster_roles_list.keys():
    
            #print(cluster_roles_list[report['cluster']['version']])
            for role in cluster_roles_list[report['cluster']['version']].keys():
                v1=client.RbacAuthorizationV1Api()
                res = eval(cluster_roles_list[report['cluster']['version']][role])

                if len(res.items) == 0 : 
                    log_pusher(log_details,cluster_name,region,role + " is not present in the cluster")
                    customer_report['cluster role'].append(role + " is not present in the cluster")
                    print(role + " is not present in the cluster")
                    not_available.append(role)
                else :
                    available.append(role)
        if len(not_available) > 0 :
            customer_report['cluster role'].append('Cluster role verification failed')
            log_pusher(log_details,cluster_name,region,'Cluster role verification failed')
            print('Cluster role verification failed')
            report['preflight_status'] = False
            errors.append('Cluster role verification failed')
            for n in not_available:
                customer_report['cluster role'].append(n + ' role is not present in the cluster')
                print(n + ' role is not present in the cluster')
                log_pusher(log_details,cluster_name,region,n + ' role is not present in the cluster')
                
        else :
            customer_report['cluster role'].append('All cluster role needed sucessfully verified')
            for n in available :
                customer_report['cluster role'].append(n + ' role is present in cluster')
                log_pusher(log_details,cluster_name,region,n + ' role is present in cluster')
                print(n + ' role is present in the cluster')
            log_pusher(log_details,cluster_name,region,'All cluster role needed sucessfully verified')
            print('All cluster role needed sucessfully verified')
        #print(report['cluster-roles'])
    except Exception as e :
        
        errors.append('Some error occured while checking the cluster roles available {err}'.format(err=e))
        customer_report['cluster role'].append('Some error occured while checking the cluster roles available {err}'.format(err=e))
        print('Some error occured while checking the cluster roles available {err}'.format(err=e))
        report['preflight_status'] = False