Create a bundle product programmatically - Magento 2

You code should like:

/** @var $objectManager \Magento\TestFramework\ObjectManager */
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager

/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->create(Magento\Catalog\Api\ProductRepositoryInterface::class);

//handle sub-items or sizes
foreach($item->Size as $size){
    //$item_name = $name."-".$size->SizeLabel;
    $sku = strtolower($size->SizeLabel) . "_" . $pID;

    /** @var \Magento\Catalog\Api\Data\ProductInterface $product */
    $product = $objectManager->create(\Magento\Catalog\Api\Data\ProductInterface::class);

    $product->setSku($sku); // Set your sku here
    $product->setName($size->SizeLabel); // Name of Product
    $product->setAttributeSetId(4);
    $product->setStatus(1); // Status on product enabled/ disabled 1/0
    $product->setWeight($size->Weight); // weight of product
    $product->setVisibility(1); // visibilty of product (catalog / search / catalog, search / Not visible individually)
    $product->setCustomAttribute('tax_class_id', 0);
    $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE); // type of product (simple/virtual/downloadable/configurable)
    $product->setPrice($size->UnitPrice); // price of product


    /** @var \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem */
    $stockItem = $objectManager->create(\Magento\CatalogInventory\Api\Data\StockItemInterface::class);
    $stockItem->setUseConfigManageStock(false);
    $stockItem->setManageStock(true);
    $stockItem->getIsInStock(true);
    $stockItem->setQty(9999);
    $productExtension->setStockItem($stockItem);

    /** @var \Magento\Catalog\Api\Data\ProductExtensionInterface $productExtension */
    $productExtension = $objectManager->create(\Magento\Catalog\Api\Data\ProductExtensionInterface::class);

    $product->setExtensionAttributes($productExtension);
    $product = $productRepository->save($product, true);



    /** @var \Magento\Bundle\Api\Data\LinkInterface $link */
    $link = $objectManager->create(\Magento\Bundle\Api\Data\LinkInterface::class);
    $link->setPosition(0);
    $link->setSku($product->getSku());
    $link->setIsDefault(false);
    $link->getQty(1);
    $link->setPrice($size->UnitPrice);
    $link->setPriceType(\Magento\Bundle\Api\Data\LinkInterface::PRICE_TYPE_FIXED);

    $links[] = $link;
}

/** @var \Magento\Catalog\Api\Data\ProductInterface $product */
$product = $objectManager->create(\Magento\Catalog\Api\Data\ProductInterface::class);

$product->setSku('sku'); // Set your sku here
//Map product with category
//$category = $this->_saveCategory($item->WebCategory);
//if ($category->getId())
//{
//    $product->setCategoryIds([$category->getId()]);
//}

$product->setName($name); // Name of Product
$product->setAttributeSetId(4);
$product->setStatus(1); // Status on product enabled/ disabled 1/0
$product->setWeight($item->Weight); // weight of product
$product->setVisibility(4); // visibilty of product (catalog / search / catalog, search / Not visible individually)
$product->setCustomAttribute('tax_class_id', 0);; // Tax class id
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE); // type of product (simple/virtual/downloadable/configurable)
$product->setPrice($item->UnitPrice); // price of product
$product->setCustomAttribute('description', $item->WebDescription); // description of product

/** @var \Magento\Catalog\Api\Data\ProductExtensionInterface $productExtension */
$productExtension = $objectManager->create(\Magento\Catalog\Api\Data\ProductExtensionInterface::class);


/** @var \Magento\Bundle\Api\Data\OptionInterface $option */
$option = $objectManager->create(\Magento\Bundle\Api\Data\OptionInterface::class);
$option->setTitle('Size');
$option->setType('radio');
$option->setRequired(true);
$option->setPosition(1);
$option->setProductLinks($links);
$productExtension->setBundleOptions([$option]);

/** @var \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem */
$stockItem = $objectManager->create(\Magento\CatalogInventory\Api\Data\StockItemInterface::class);
$stockItem->setUseConfigManageStock(false);
$stockItem->setManageStock(true);
$stockItem->getIsInStock(true);
$stockItem->setQty(9999);
$productExtension->setStockItem($stockItem);

$product->setExtensionAttributes($productExtension);
$productRepository->save($product, true);

Magento2 programmatically adding a custom option

I propose to look at this post - which provides much easier answer and first of all working one. Look at /dev/tests/integration/testsuite/Magento/Bundle/_files/product.php

There is a script adding bundle product with one option and one linked product.