All files / app/assets/javascripts/clusters_list/components agent_table.vue

94.73% Statements 36/38
89.47% Branches 17/19
100% Functions 18/18
94.59% Lines 35/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363                                          4x                                                                                           48x             48x 48x                                                                             48x       48x 241x 241x       48x     1x     1x 1x         828x     600x     186x     414x       241x   241x       241x   241x     1278x     1480x                     1020x   864x 864x   864x 864x 40x     864x       186x 46x   140x 44x   96x 96x                                                                                                                                                                                                                                                                                                                                              
<script>
import {
  GlLink,
  GlTable,
  GlIcon,
  GlSprintf,
  GlTooltip,
  GlTooltipDirective,
  GlPopover,
  GlBadge,
  GlPagination,
} from '@gitlab/ui';
import semverLt from 'semver/functions/lt';
import semverInc from 'semver/functions/inc';
import semverPrerelease from 'semver/functions/prerelease';
import TimeAgoTooltip from '~/vue_shared/components/time_ago_tooltip.vue';
import timeagoMixin from '~/vue_shared/mixins/timeago';
import { helpPagePath } from '~/helpers/help_page_helper';
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
import { MAX_LIST_COUNT, AGENT_STATUSES, I18N_AGENT_TABLE } from '../constants';
import { getAgentConfigPath } from '../clusters_util';
import DeleteAgentButton from './delete_agent_button.vue';
 
export default {
  i18n: I18N_AGENT_TABLE,
  components: {
    GlLink,
    GlTable,
    GlIcon,
    GlSprintf,
    GlTooltip,
    GlPopover,
    GlBadge,
    GlPagination,
    TimeAgoTooltip,
    DeleteAgentButton,
  },
  directives: {
    GlTooltip: GlTooltipDirective,
  },
  mixins: [timeagoMixin],
  AGENT_STATUSES,
  troubleshootingLink: helpPagePath('user/clusters/agent/troubleshooting'),
  versionUpdateLink: helpPagePath('user/clusters/agent/install/index', {
    anchor: 'update-the-agent-version',
  }),
  configHelpLink: helpPagePath('user/clusters/agent/install/index', {
    anchor: 'create-an-agent-configuration-file',
  }),
  inject: ['kasVersion'],
  props: {
    agents: {
      required: true,
      type: Array,
    },
    defaultBranchName: {
      default: '.noBranch',
      required: false,
      type: String,
    },
    maxAgents: {
      default: null,
      required: false,
      type: Number,
    },
  },
  data() {
    return {
      currentPage: 1,
      limit: this.maxAgents ?? MAX_LIST_COUNT,
    };
  },
  computed: {
    fields() {
      const tdClass = 'gl-pt-3! gl-pb-4! gl-vertical-align-middle!';
      return [
        {
          key: 'name',
          label: this.$options.i18n.nameLabel,
          tdClass,
        },
        {
          key: 'status',
          label: this.$options.i18n.statusLabel,
          tdClass,
        },
        {
          key: 'lastContact',
          label: this.$options.i18n.lastContactLabel,
          tdClass,
        },
        {
          key: 'version',
          label: this.$options.i18n.versionLabel,
          tdClass,
        },
        {
          key: 'agentID',
          label: this.$options.i18n.agentIdLabel,
          tdClass,
        },
        {
          key: 'configuration',
          label: this.$options.i18n.configurationLabel,
          tdClass,
        },
        {
          key: 'options',
          label: '',
          tdClass,
        },
      ];
    },
    agentsList() {
      Iif (!this.agents.length) {
        return [];
      }
 
      return this.agents.map((agent) => {
        const versions = this.getAgentVersions(agent);
        return { ...agent, versions };
      });
    },
    showPagination() {
      return !this.maxAgents && this.agents.length > this.limit;
    },
    prevPage() {
      return Math.max(this.currentPage - 1, 0);
    },
    nextPage() {
      const nextPage = this.currentPage + 1;
      return nextPage > Math.ceil(this.agents.length / this.limit) ? null : nextPage;
    },
  },
  methods: {
    getStatusCellId(item) {
      return `connection-status-${item.name}`;
    },
    getVersionCellId(item) {
      return `version-${item.name}`;
    },
    getPopoverTestId(item) {
      return `popover-${item.name}`;
    },
    getAgentId(item) {
      return getIdFromGraphQLId(item.id);
    },
    getAgentConfigPath,
    getAgentVersions(agent) {
      const agentConnections = agent.connections?.nodes || [];
 
      const agentVersions = agentConnections.map((agentConnection) =>
        agentConnection.metadata.version.replace('v', ''),
      );
 
      const uniqueAgentVersions = [...new Set(agentVersions)];
 
      return uniqueAgentVersions.sort((a, b) => a.localeCompare(b));
    },
    getAgentVersionString(agent) {
      return agent.versions[0] || '';
    },
    isVersionMismatch(agent) {
      return agent.versions.length > 1;
    },
    // isVersionOutdated determines if the agent version is outdated compared to the KAS / GitLab version
    // using the following heuristics:
    // - KAS Version is used as *server* version if available, otherwise the GitLab version is used.
    // - returns `outdated` if the agent has a different major version than the server
    // - returns `outdated` if the agents minor version is at least two proper versions older than the server
    //   - *proper* -> not a prerelease version. Meaning that server prereleases (with `-rcN`) suffix are counted as the previous minor version
    //
    // Note that it does NOT support if the agent is newer than the server version.
    isVersionOutdated(agent) {
      if (!agent.versions.length) return false;
 
      const agentVersion = this.getAgentVersionString(agent);
      let allowableAgentVersion = semverInc(agentVersion, 'minor');
 
      const isServerPrerelease = Boolean(semverPrerelease(this.kasVersion));
      if (isServerPrerelease) {
        allowableAgentVersion = semverInc(allowableAgentVersion, 'minor');
      }
 
      return semverLt(allowableAgentVersion, this.kasVersion);
    },
 
    getVersionPopoverTitle(agent) {
      if (this.isVersionMismatch(agent) && this.isVersionOutdated(agent)) {
        return this.$options.i18n.versionMismatchOutdatedTitle;
      }
      if (this.isVersionMismatch(agent)) {
        return this.$options.i18n.versionMismatchTitle;
      }
      if (this.isVersionOutdated(agent)) {
        return this.$options.i18n.versionOutdatedTitle;
      }
 
      return null;
    },
  },
};
</script>
 
<template>
  <div>
    <gl-table
      :items="agentsList"
      :fields="fields"
      :per-page="limit"
      :current-page="currentPage"
      stacked="md"
      class="gl-mb-4!"
      data-testid="cluster-agent-list-table"
    >
      <template #cell(name)="{ item }">
        <gl-link :href="item.webPath" data-testid="cluster-agent-name-link">{{ item.name }}</gl-link
        ><gl-badge v-if="item.isShared" class="gl-ml-3">{{
          $options.i18n.sharedBadgeText
        }}</gl-badge>
      </template>
 
      <template #cell(status)="{ item }">
        <span
          :id="getStatusCellId(item)"
          class="gl-md-pr-5"
          data-testid="cluster-agent-connection-status"
        >
          <span :class="$options.AGENT_STATUSES[item.status].class" class="gl-mr-3">
            <gl-icon :name="$options.AGENT_STATUSES[item.status].icon" :size="16" /></span
          >{{ $options.AGENT_STATUSES[item.status].name }}
        </span>
        <gl-tooltip
          v-if="item.status === 'active'"
          :target="getStatusCellId(item)"
          placement="right"
        >
          <gl-sprintf :message="$options.AGENT_STATUSES[item.status].tooltip.title"
            ><template #timeAgo>{{ timeFormatted(item.lastContact) }}</template>
          </gl-sprintf>
        </gl-tooltip>
        <gl-popover
          v-else
          :target="getStatusCellId(item)"
          :title="$options.AGENT_STATUSES[item.status].tooltip.title"
          placement="right"
          container="viewport"
        >
          <p>
            <gl-sprintf :message="$options.AGENT_STATUSES[item.status].tooltip.body"
              ><template #timeAgo>{{ timeFormatted(item.lastContact) }}</template></gl-sprintf
            >
          </p>
          <p class="gl-mb-0">
            <gl-link :href="$options.troubleshootingLink" target="_blank" class="gl-font-sm">
              {{ $options.i18n.troubleshootingText }}</gl-link
            >
          </p>
        </gl-popover>
      </template>
 
      <template #cell(lastContact)="{ item }">
        <span data-testid="cluster-agent-last-contact">
          <time-ago-tooltip v-if="item.lastContact" :time="item.lastContact" />
          <span v-else>{{ $options.i18n.neverConnectedText }}</span>
        </span>
      </template>
 
      <template #cell(version)="{ item }">
        <span :id="getVersionCellId(item)" data-testid="cluster-agent-version">
          {{ getAgentVersionString(item) }}
 
          <gl-icon
            v-if="isVersionMismatch(item) || isVersionOutdated(item)"
            name="warning"
            class="gl-text-orange-500 gl-ml-2"
          />
        </span>
 
        <gl-popover
          v-if="isVersionMismatch(item) || isVersionOutdated(item)"
          :target="getVersionCellId(item)"
          :title="getVersionPopoverTitle(item)"
          :data-testid="getPopoverTestId(item)"
          placement="right"
          container="viewport"
        >
          <div v-if="isVersionMismatch(item) && isVersionOutdated(item)">
            <p>{{ $options.i18n.versionMismatchText }}</p>
 
            <p class="gl-mb-0">
              <gl-sprintf :message="$options.i18n.versionOutdatedText">
                <template #version>{{ kasVersion }}</template>
              </gl-sprintf>
              <gl-link :href="$options.versionUpdateLink" class="gl-font-sm">
                {{ $options.i18n.viewDocsText }}</gl-link
              >
            </p>
          </div>
          <p v-else-if="isVersionMismatch(item)" class="gl-mb-0">
            {{ $options.i18n.versionMismatchText }}
          </p>
 
          <p v-else-if="isVersionOutdated(item)" class="gl-mb-0">
            <gl-sprintf :message="$options.i18n.versionOutdatedText">
              <template #version>{{ kasVersion }}</template>
            </gl-sprintf>
            <gl-link :href="$options.versionUpdateLink" class="gl-font-sm">
              {{ $options.i18n.viewDocsText }}</gl-link
            >
          </p>
        </gl-popover>
      </template>
 
      <template #cell(agentID)="{ item }">
        <span data-testid="cluster-agent-id">
          {{ getAgentId(item) }}
        </span>
      </template>
 
      <template #cell(configuration)="{ item }">
        <span data-testid="cluster-agent-configuration-link">
          <gl-link v-if="item.configFolder" :href="item.configFolder.webPath">
            {{ getAgentConfigPath(item.name) }}
          </gl-link>
 
          <span v-else-if="item.isShared">
            {{ $options.i18n.externalConfigText }}
          </span>
 
          <span v-else
            >{{ $options.i18n.defaultConfigText }}
            <gl-link
              v-gl-tooltip
              :href="$options.configHelpLink"
              :title="$options.i18n.defaultConfigTooltip"
              :aria-label="$options.i18n.defaultConfigTooltip"
              class="gl-vertical-align-middle"
              ><gl-icon name="question-o" :size="14" /></gl-link
          ></span>
        </span>
      </template>
 
      <template #cell(options)="{ item }">
        <delete-agent-button
          v-if="!item.isShared"
          :agent="item"
          :default-branch-name="defaultBranchName"
        />
      </template>
    </gl-table>
 
    <gl-pagination
      v-if="showPagination"
      v-model="currentPage"
      :prev-page="prevPage"
      :next-page="nextPage"
      align="center"
      class="gl-mt-5"
    />
  </div>
</template>